当命令被布置和连接直接定义在命令连接是否紧密? [英] Does connection close when command is disposed and the connection is defined directly on the command?

查看:118
本文介绍了当命令被布置和连接直接定义在命令连接是否紧密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道了很多例子表明一个SqlConnection定义,然后一个SqlCommand定义,无论是在使用块:

 使用(VAR康恩=新的SqlConnection(CONNSTRING)){
使用(VAR CMD =新的SqlCommand()){
cmd.Connection =康恩;
//打开连接
}
}



我的问题:如果我直接定义中SqlCommand的连接,确实当命令被设置在连接关闭

 使用(VAR CMD =新的SqlCommand()){
cmd.Connection =新的SqlConnection(CONNSTRING);
//打开连接
}


解决方案

没有,从来没有的SqlCommand试图关闭连接/处置。


I know that a lot of examples exist where a SqlConnection is defined and then a SqlCommand is defined, both in Using blocks:

using (var conn = new SqlConnection(connString)) {
      using (var cmd = new SqlCommand()) {
        cmd.Connection = conn;
        //open the connection
      }
}

My question: If I define the connection directly on the SqlCommand, does the connection close when the command is disposed?

using (var cmd = new SqlCommand()) {
      cmd.Connection = new SqlConnection(connString);
      //open the connection
}

解决方案

No, SqlCommand never attempts to close/dispose of the connection.

这篇关于当命令被布置和连接直接定义在命令连接是否紧密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆