开发者

Closing database connection in asp.net

开发者 https://www.devze.com 2023-02-13 03:14 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号