简体   繁体   中英

Will a using block close a database connection?

using (DbConnection conn = new DbConnection())
{
    // do stuff with database
}

Will the using 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.

肯定是的,因为它会处理连接,并且在处理连接的内部逻辑之前会调用关闭。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM