使用上SqlDataReader的 [英] using on SQLDataReader

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

问题描述

我知道我问了一个相关的问题早。我只是另一个想法。

I know I asked a related question earlier. I just had another thought.

using (SqlConnection conn = new SqlConnection('blah blah'))
{
    using(SqlCommand cmd = new SqlCommand(sqlStatement, conn))
    {
        conn.open();

        // *** do I need to put this in using as well? ***
        SqlDataReader dr = cmd.ExecuteReader() 
        {
            While(dr.Read())
            {
                //read here
            }
        }
    }
}

的理由是:由于在 SqlDataReader的 博士的对象不是一个新对象,像连接或命令对象,它只是一个参考指向 cmd.ExecuteReader()方法,我需要把读者在使用。 (现在根据我以前的帖子,这是我的理解是,它使用的任何对象的IDisposable 需要被放在一个使用 SqlDataReader的的IDisposable ,所以我需要把它。我是正确的我的判断吗?)我因为它不是一个新的对象,我只是困惑,它会导致配置的对象,仅仅是一个参考指针,指向命令什么问题?

The argument is that: Since the SqlDataReader dr object is NOT A NEW OBJECT LIKE THE connection or command objects, its simply a reference pointing to the cmd.ExecuteReader() method, do I need to put the reader inside a using. (Now based on my previous post, it is my understanding that any object that uses IDisposable needs to be put in a using, and SQLDataReader inherits from IDisposable, so I need to put it. Am I correct in my judgement?) I am just confused since its not a new object, would it cause any problems in disposing an object that simply is a reference pointer to the command?

非常感谢

推荐答案

我想你就错了。在博士是由 cmd.ExecuteReader 返回的对象,这将是一个新的对象的引用。在你的榜样,什么都不会出售博士,所以是的,它需要在一个使用,或手动处理。

I think you are mistaken. The dr is a reference to the object returned by cmd.ExecuteReader, which is going to be a new object. In your example, nothing will dispose dr, so yes it needs to be in a using, or manually disposed.

您关于的IDisposable 实施者<判断EM>需要的是在一个使用是不正确的。他们将发挥罚款之外。 A 使用语句只是语法糖的尝试......终于。实施东西的IDisposable 的Dispose 调用,因为它们的信号,他们需要以一种确定的方式来处理特定的状态。

Your judgement about IDisposable implementors needing to be in a using is not correct. They will function fine outside. A using statement is just syntactic sugar for a try ... finally. Things implementing IDisposable should have Dispose called, because they are signalling that they need to dispose certain state in a deterministic way.

请注意,如果你不叫的Dispose ,它并不总是一个问题。有些对象也实现了终结,这将是由垃圾收集器被触发。如果他们不执行终结,他们可能会离开非托管内存未回收。直到你的应用程序关闭这将保持未回收。所有的托管内存最终被收回,除非是不符合使用垃圾收集

Note that if you do not call Dispose, its not always a problem. Some objects also implement the finalizer, which will be triggered by the garbage collector. If they do not implement the finalizer, they might leave unmanaged memory unreclaimed. This will remain unreclaimed until your application closes. All managed memory is eventually reclaimed, unless it is not elligible for garbage collection.

重新编写:

using (SqlConnection conn = new SqlConnection('blah blah')) 
using(SqlCommand cmd = new SqlCommand(sqlStatement, conn)) 
{
   conn.open(); 
   using (SqlDataReader dr = cmd.ExecuteReader())
   { 
        while (dr.Read()) 
        { 
           //read here 
        } 
   } 
} 

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

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