会不会把一个"使用"围绕一个DataReader声明关闭它? [英] Will putting a "using" statement around a DataReader close it?

查看:126
本文介绍了会不会把一个"使用"围绕一个DataReader声明关闭它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常写我的的DataReader code是这样的:

 尝试
{
    博士= cmd.ExecuteReader(CommandBehavior.SingleResult);
    而(dr.Read())
    {
        //做的东西
    }
}
最后
{
    如果(!博士= NULL){dr.Close(); }
}
 

是否安全,以取代尝试最后只是一个使用围绕的DataReader 的创作块?我不知道的原因是因为所有我见过他们使用微软的例子中使用的连接,但始终显式调用关闭() DataReader的

继承人是从一个例子 检索数据使用DataReader(ADO.NET)

 静态无效HasRows(SqlConnection的连接)
{
    使用(连接)
    {
        SqlCommand的命令=新的SqlCommand(
          选择类别ID,类别名称从类别;
          连接);
        connection.Open();

        SqlDataReader的读卡器= Command.ExecuteReader却();

        如果(reader.HasRows)
        {
            而(reader.Read())
            {
                Console.WriteLine({0} \吨{1},reader.GetInt32(0),
                    reader.GetString(1));
            }
        }
        其他
        {
            Console.WriteLine(没有找到行。);
        }
        reader.Close();
    }
}
 

解决方案

是的。 使用调用Dispose。调用Dispose上SqlDataReader的关闭它。

这是SqlDataReader对象的psuedo- code,从收集到的反射

 公共无效的Dispose()
    {
        this.Close();
    }

    公众覆盖无效关闭()
    {
        如果(!isClosed返)
            CloseInternal(真正的);
    }

    私人无效CloseInternal(布尔closeReader)
    {
        尝试
        {
            //做一些东西来关闭阅读器本身
        }
        赶上(例外前)
        {
            this.Connection.Abort();
            扔;
        }

        如果(this.Connection = NULL和放大器;!&安培; CommandBehavior.CloseConnection ==真)
        {
            this.Connection.Close();
        }
    }
 

I usually write my DataReader code like this:

try
{
    dr = cmd.ExecuteReader(CommandBehavior.SingleResult);
    while (dr.Read())
    {
        // Do stuff
    }
}
finally
{
    if (dr != null) { dr.Close(); }
}

Is it safe to replace the try and finally with just a using block around the DataReader's creation? The reason I wonder is because in all the Microsoft examples I've seen they use a using for the connection but always explicitly call Close() on the DataReader.

Heres's an example from Retrieving Data Using a DataReader (ADO.NET):

static void HasRows(SqlConnection connection)
{
    using (connection)
    {
        SqlCommand command = new SqlCommand(
          "SELECT CategoryID, CategoryName FROM Categories;",
          connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
                    reader.GetString(1));
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        reader.Close();
    }
}

解决方案

Yes. using calls Dispose. Calling Dispose on SqlDataReader closes it.

This is psuedo-code of SqlDataReader gleaned from Reflector:

    public void Dispose()
    {
        this.Close();
    }

    public override void Close()
    {
        if( !IsClosed )
            CloseInternal(true);
    }

    private void CloseInternal(bool closeReader)
    {
        try
        {
            // Do some stuff to close the reader itself
        }
        catch(Exception ex)
        {
            this.Connection.Abort();
            throw;
        }

        if( this.Connection != null && CommandBehavior.CloseConnection == true )
        {
            this.Connection.Close();
        }
    }

这篇关于会不会把一个"使用"围绕一个DataReader声明关闭它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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