开发者

Will a using block close a database connection?

开发者 https://www.devze.com 2023-02-15 21:44 出处:网络
using (DbConnection conn = new DbConnection()) { // do stuff with database } Will the u开发者_运维问答sing block call conn.Close()?Yes, it will; the implementation of DbConnection.Dispose() calls Cl
using (DbConnection conn = new DbConnection())
{
    // do stuff with database
}

Will the u开发者_运维问答sing block call conn.Close()?


Yes, it will; the implementation of DbConnection.Dispose() calls Close() (and so do its derived implementations).


Yes - http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close.aspx

edit: from Microsoft: "The connection is automatically closed at the end of the using block."


A using block will ensure the destruction of DbConnection object by calling the Dispose() method. The Dispose() method will in turn call the Close() method and has to wait for it to finish closing the connection to the database.


surely yes because it will dispose the connection and before disposing the inner logic of the connection calls the close.

0

精彩评论

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

关注公众号