SqlCommand.Dispose 是否关闭连接?
我可以有效地使用这种方法吗?
using(SqlCommand cmd = new SqlCommand("GetSomething", new SqlConnection(Config.ConnectionString))
{
cmd.Connection.Open();
// set up parameters and CommandType to StoredProcedure etc. etc.
cmd.ExecuteNonQuery();
}
我担心的是:SqlCommand 的 Dispose 方法(退出 using 块时调用)是否会关闭底层 SqlConnection 对象?
Can I use this approach efficiently?
using(SqlCommand cmd = new SqlCommand("GetSomething", new SqlConnection(Config.ConnectionString))
{
cmd.Connection.Open();
// set up parameters and CommandType to StoredProcedure etc. etc.
cmd.ExecuteNonQuery();
}
My concern is : Will the Dispose method of the SqlCommand (which is called when exiting the using block) close the underlying SqlConnection object or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,处置
SqlCommand
不会影响连接。 更好的方法是将SqlConnection
也包装在 using 块中:否则,由于使用它的 Command 已被释放,Connection 不会发生变化(也许这就是您想要的? )。 但请记住,连接应该
也可以被处理掉,并且可能比命令更重要。
编辑:
我刚刚测试了这一点:
退出 using 块时第一个命令被释放。 连接仍然打开并且适合第二个命令。
因此,处置该命令绝对不会处置它正在使用的连接。
No, Disposing of the
SqlCommand
will not effect the Connection. A better approach would be to also wrap theSqlConnection
in a using block as well:Otherwise, the Connection is unchanged by the fact that a Command that was using it was disposed (maybe that is what you want?). But keep in mind, that a Connection should
be disposed of as well, and likely more important to dispose of than a command.
EDIT:
I just tested this:
The first command was disposed when the using block was exited. The connection was still open and good for the second command.
So, disposing of the command definitely does not dispose of the connection it was using.
SqlCommand.Dispose 是不够的,因为许多 SqlCommand 可以(重新)使用相同的 SqlConnection。 将您的注意力集中在 SqlConnection 上。
SqlCommand.Dispose will not be sufficient because many SqlCommand(s) can (re)use the same SqlConnection. Center your focus on the SqlConnection.
很多地方都犯了这个错误,甚至是微软自己的文档。 请记住 - 在数据库世界中,几乎所有东西都由非托管资源支持,因此几乎所有东西都实现了 IDisposable。 假设一个类这样做,除非编译器另有说明。
将您的命令包装在 using 中。 将您的连接封装在 using 中。 创建 DbProvider 的连接(从 DbProviderFactories.GetFactory 获取),并创建连接的命令,这样,如果您更改底层数据库,则只需更改对 DBPF.GetFactory 的调用。
所以你的代码最终应该看起来漂亮且对称:
Soooo many places get this wrong, even MS' own documentation. Just remember - in DB world, almost everything is backed by an unmanaged resource, so almost everything implements IDisposable. Assume a class does unless the compiler tells you otherwise.
Wrap your command in a using. Wrap your connection in a using. Create your connection off a DbProvider (get that from DbProviderFactories.GetFactory), and your command off your connection so that if you change your underlying DB, you only need to change the call to DBPF.GetFactory.
So your code should end up looking nice and symmetrical:
我用这个模式。 我的应用程序中的某处有这个私有方法:
然后我总是在 try 块中创建 SQL 命令和连接(但不包含在 using 块中),并且始终有一个 finally 块:
连接对象是命令对象的属性在这种情况下,使用块会很尴尬 - 但这种模式可以完成工作,而不会弄乱您的代码。
I use this pattern. I have this private method somewhere in my app:
Then I always create SQL commands and connections in a try block (but without being wrapped in a using block) and always have a finally block as:
The connection object being a property of the command object makes a using block awkward in this situation - but this pattern gets the job done without cluttering up your code.