Can you reuse SqlConnection?

Can you reuse SqlConnection?

To answer your specific question, you can reuse a SqlConnection for each query. Just make sure to close your current query ( SqlDataReader , etc.) before you run another one, ie. wrap them in their own using blocks.

Should you keep database connection open?

For fast response time and high throughput, it’s actually best to keep database connections open and reuse them for subsequent requests. Most database frameworks offer some kind of connection pool mechanism where a request handler can get a database connection for its work and return it to the pool afterwards.

When should you use Oledb connection object?

When to use OleDbConnection? When the connection is established between C# application and the specified data source, SQL commands will execute with the help of the Connection Object and retrieve or manipulate the data in the database.

When should I close database connection?

2 Answers. For the purpose of safe coding, you should always close database connections explicitly to make sure that the code was able to close itself gracefully and to prevent any other objects from reusing the same connection after you are done with it.

How can I use one database connection object in whole?

Now all I want to do is instantiate this class once and get connection object. And I want to use this same object in entire application. Another solution will also be appreciated. but, you should think – how this will work in multi-threaded environment, when several threads are trying to make requests to database.

How to minimize number of database connection objects?

In other words, rather than thinking in terms of minimizing the number of connection objects you create (by re-using one connection for an application or http request), think in terms of one connection for each time you call out to the database… each round trip.

Is it good or bad to create database connections?

Note that without connection pooling, one connection per query would be a very bad idea because creating real database connections can be very costly (authentication, network overhead etc.), and the number of simultaneous open connections is usually very limited. Developers sometimes want to “optimize” their code to re-use their connection objects.

Do you need to connect to a different database every time?

I added ability to connect to a different database every time, in some cases, like ours, it’s what you need to do on the fly. Hope this helps. in any class because it is static method. Not the answer you’re looking for?