是否结束使用关闭打开的 SQL 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/376068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does End Using close an open SQL Connection
提问by rjrapson
If I wrap a SQLConnection in a Using, should I close it or does the end using handle it?
如果我将 SQLConnection 包装在 Using 中,我应该关闭它还是最终 using 处理它?
using cn as new system.data.sqlclient.sqlconnection()
cn.open
'{do a bunch of other stuff with commands and datareaders here}
cn.close 'Do I need this?
end using
回答by matt b
Exiting a using block calls .Dispose() on the object in question (cn
in your example) which for a SqlConnection will close the connection and any open resources.
退出 using 块对相关对象(cn
在您的示例中)调用 .Dispose() ,对于 SqlConnection 将关闭连接和任何打开的资源。
回答by Darin Dimitrov
More precisely calling Dispose or Close will mark the underlying physical connection as "Not in use" - but doesn't really close it. A "Not in use" connection that isn't yet physically closed is thus available for pooling. Therefore - calling Dispose would return a connection to the connection pool.
更准确地说,调用 Dispose 或 Close 会将底层物理连接标记为“未使用”——但不会真正关闭它。因此,尚未物理关闭的“未使用”连接可用于池化。因此 - 调用 Dispose 将返回到连接池的连接。
回答by Hector Correa
According to MSDN you don't need the close statement.
根据 MSDN,您不需要 close 语句。
"The following example creates a SqlConnection, opens it, displays some of its properties. The connection is automatically closed at the end of the using block." -- http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close.aspx
“以下示例创建一个 SqlConnection,打开它,显示它的一些属性。连接在 using 块结束时自动关闭。” -- http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close.aspx
回答by Bryan Anderson
While the SQL's Dispose method does close the connection (eventually according to darin) you should leave the call to Close in there. The reason is that you would be relying on the underlying implementation of Dispose to call close. Also seeing an Open without a Close is like seeing a New without a Delete for those of us that have programmed in unmanaged languages. It's a code smell for me.
虽然 SQL 的 Dispose 方法确实关闭了连接(最终根据 darin),但您应该在那里保留对 Close 的调用。原因是您将依赖于 Dispose 的底层实现来调用 close。对于我们这些用非托管语言编程的人来说,看到一个没有关闭的打开就像看到一个没有删除的新。这对我来说是一种代码气味。
回答by michaeldavid
"A Using block behaves like a Try...Finally construction in which the Try block uses the resources and the Finally block disposes of them. Because of this, the Using block guarantees disposal of the resources, no matter how you exit the block. This is true even in the case of an unhandled exception, except for a StackOverflowException."
https://msdn.microsoft.com/en-us/library/htd05whh.aspx
“Using 块的行为类似于 Try...Finally 构造,其中 Try 块使用资源,而 finally 块处理它们。因此,Using 块保证资源的处理,无论您如何退出该块。即使在未处理的异常的情况下也是如此,除了 StackOverflowException。”
https://msdn.microsoft.com/en-us/library/htd05whh.aspx
回答by Matt Briggs
using is just a shorthand to try/finally. this is equivilent code to what you posted
using 只是尝试/最终的速记。这是与您发布的内容等效的代码
Try
SqlConnection cn as new system.data.sqlclient.sqlconnection()
cn.open
'{do a bunch of other stuff with commands and datareaders here}
cn.close 'Do I need this?
Finally
cn.Dispose()
End Try
Dispose is supposed to take care of all resource cleanup, in the case of connections it will close it.
Dispose 应该负责所有资源清理,在连接的情况下它将关闭它。