简体   繁体   English

using 块会关闭数据库连接吗?

[英]Will a using block close a database connection?

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

Will the using block call conn.Close() ? using块会调用conn.Close()吗?

Yes, it will;是的,它会; the implementation of DbConnection.Dispose() calls Close() (and so do its derived implementations). DbConnection.Dispose()的实现调用Close() (以及它的派生实现)。

Yes - http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close.aspx是 - 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. using块将通过调用Dispose()方法确保销毁DbConnection对象。 The Dispose() method will in turn call the Close() method and has to wait for it to finish closing the connection to the database. Dispose()方法将依次调用Close()方法,并且必须等待它完成关闭与数据库的连接。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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