I'm looking for a way to improve my to access the database from WebParts.

Right now, each and every single time i need to get/set some data from the database (either with linq or with System.Data.SqlClient), I:

  • Retrieve the connection string from web config
  • Establish a new connection (time consuming)
  • Execute
  • Close connection (time consuming)
  • Release used objects

Is there a better way, e.g. establish a single connection on page load and "reuse" it on every calls, or use the same used by the sharepoint unlying application?

Thank you!

  • what about caching the connection?
    – Nk SP
    Aug 18, 2015 at 10:21

1 Answer 1


If you create one per query / transaction, it is much easier to manage "closing" the connections.

I can see why common sense dictates that you should open one and use it throughout, but you will run into problems with dropped connections and multi threading. So your next step will be to open a pool, say of 50, connections and keep them all open, doling them out to different processes. And then you'll find out that this is exactly what the .NET framework does for you already.

If you open a connection when you need it and dispose of it when you've finished, that will not actually close the connection, it'll just return it to the connection pool to be used again.

Source: Accepted answer of: Creating database connections - Do it once or for each query?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.