是SqlCommand.Dispose()必需的,如果关联的SqlConnection将被全部销毁? [英] Is SqlCommand.Dispose() required if associated SqlConnection will be disposed?

查看:1309
本文介绍了是SqlCommand.Dispose()必需的,如果关联的SqlConnection将被全部销毁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用code是这样的:

I usually use code like this:

using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString))
{
   var command = connection.CreateCommand();
   command.CommandText = "...";
   connection.Open();
   command.ExecuteNonQuery();
}

请问我的命令自动处理?还是不行,我必须用包装成 块?是否需要处置的SqlCommand

Will my command automatically disposed? Or not and I have to wrap it into using block? Is it required to dispose SqlCommand?

推荐答案

只要做到这一点:

using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString))
using(var command = connection.CreateCommand())
{
   command.CommandText = "...";
   connection.Open();
   command.ExecuteNonQuery();
}

不调用处理的命令不会做任何事太糟糕了。然而它调用Dispose将苏preSS调用终结,使得呼叫处理性能增强。

Not calling dispose on the command won't do anything too bad. However calling Dispose on it will supress the call to the finalizer, making calling dispose a performance enhancement.

这篇关于是SqlCommand.Dispose()必需的,如果关联的SqlConnection将被全部销毁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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