SqlCommand时使用using语句 [英] SqlCommand with using statement

查看:402
本文介绍了SqlCommand时使用using语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到,大多数样品中的SqlCommand使用这样

 使用(SqlConnection的CON =新的SqlConnection(CNN_STRING))
{
使用(CMD的SqlCommand =新的SqlCommand(选择ID,名称从人,CON))
{
SqlDataAdapter的大=新SqlDataAdapter的(CMD);
的DataSet DS =新的DataSet();
da.Fill(DS);
返回DS;
}
}



我知道为什么我们使用的是使用的声明。但是的SqlCommand不inlcude Close()方法,所以要我们真的在using语句


解决方案

由于它也实现了<用它一类=文档链接HREF =htt​​p://stackoverflow.com/documentation/c%23/1795/idisposable-interface/8699/using-keyword-and-idisposable#t=201608091326244960503> IDisposable的



使用声明的目的是,当控制将达到使用到底会处置该对象使用块并释放内存。其目的不仅是自动连接紧密,基本上就会处理连接对象,显然连接也被关闭,由于它。



其目的是腾出的资源,我们Using语句内使用。



据的 MSDN:




作为一个规则,当您使用的IDisposable 对象,你应该声明,并在声明中使用实例化它。 using语句调用正确的方式在物体上的的Dispose 法,(当你使用它作为前面所示),这也使得对象本身走出去的范围,因为一旦的Dispose 被调用。在using块,对象是只读的,不能进行修改或重新分配。



using语句确保了的Dispose 叫即使当您正在调用对象的方法发生异常。您可以通过将对象的try块中,然后调用的Dispose 最后块达到同样的效果;其实,这是怎样的using语句是由编译器翻译。该代码示例前面扩展到在编译时以下代码(注意额外的花括号来创建对象的范围有限):




注意:




您可以实例化资源对象,然后传递变量using语句,但这并不是最好的做法。在这种情况下,对象仍然控制范围离开using块后,即使它可能不再有机会获得它的非托管资源。换言之,它将不再被完全初始化。如果您尝试使用对象的使用外块,你就有可能引起异常被抛出。出于这个原因,所以一般最好实例化对象在using语句,并限制其范围的使用块。



I saw that in most samples SqlCommand was used like this

using (SqlConnection con = new SqlConnection(CNN_STRING))
{
    using (SqlCommand cmd = new SqlCommand("Select ID,Name From Person", con))
    {
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);           
        return ds;
    }
}

I know why we are using "using" statement. But SqlCommand doesn't inlcude Close() method, so should we really use it within using statement

解决方案

Because it also implements IDisposable.

The purpose of Using statement is that when control will reach end of using it will dispose that object of using block and free up memory. its purpose is not only for auto connection close, basically it will dispose connection object and obviously connection also closed due to it.

Its purpose is to free up the resources that we used inside the Using statement.

According to MSDN:

As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object):

NOTE:

You can instantiate the resource object and then pass the variable to the using statement, but this is not a best practice. In this case, the object remains in scope after control leaves the using block even though it will probably no longer have access to its unmanaged resources. In other words, it will no longer be fully initialized. If you try to use the object outside the using block, you risk causing an exception to be thrown. For this reason, it is generally better to instantiate the object in the using statement and limit its scope to the using block.

这篇关于SqlCommand时使用using语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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