【发布时间】:2019-02-20 17:53:39
【问题描述】:
当 SqlConnection 和 SqlCommand 都在单个 using 块中创建时,都在退出时被释放,还是需要嵌套 using 块?
using (SqlCommand command = new SqlConnection(ConnectionString).CreateCommand()) {
// Use command..
}
【问题讨论】:
-
是的,您需要在两者上都使用块。 using 块仅适用于该对象,而不适用于可能在其中创建的其他对象。您在这里所拥有的只会处理命令,连接会陷入困境。
标签: c# sql-server ado.net