Closing database connection in asp.net
Can we close all known/unknown conne开发者_如何学Cctions to database with the code?
I'm using Access database and my application gives the following error: "Could not use ''; file already in use. "
I don't know which connection is opened and no closed, so is there a way to close all application's opened connections?
When working with disposable objects you should use using
so they will get disposed, and in this case even closed, when leaving the using
block. Your code should look something like:
using (var connection = new OleDbConnection(connectionString))
{
connection.Open();
// Do work here; connection closed on following line.
}
Read about OleDbConnection.
UPDATE: I missed that you were accessing an access database, so updated the code to use OleDbConnection
instead.
精彩评论