using 块会关闭数据库连接吗?
using (DbConnection conn = new DbConnection())
{
// do stuff with database
}
using
块会调用 conn.Close()
吗?
using (DbConnection conn = new DbConnection())
{
// do stuff with database
}
Will the using
block call conn.Close()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,会的;
DbConnection.Dispose()
的实现调用Close()
(其派生实现也是如此)。Yes, it will; the implementation of
DbConnection.Dispose()
callsClose()
(and so do its derived implementations).是 - http://msdn.microsoft.com /en-us/library/system.data.sqlclient.sqlconnection.close.aspx
编辑:来自 Microsoft:“连接在 using 块末尾自动关闭。”
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."
using
块将确保通过调用Dispose()
方法销毁DbConnection
对象。Dispose()
方法将依次调用Close()
方法,并且必须等待它完成关闭与数据库的连接。A
using
block will ensure the destruction ofDbConnection
object by calling theDispose()
method. TheDispose()
method will in turn call theClose()
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.