什么是从的DbDataReader读取数据的最快方法是什么? [英] What is the fastest way to read data from a DbDataReader?

查看:1207
本文介绍了什么是从的DbDataReader读取数据的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下code,命令是已经成立了的DbCommand:

 使用(VAR的DataReader = Command.ExecuteReader却()/ *查询的实际执行花费相对较少的时间。* /){
                而(dataReader.Read()){
                    //这些是采取所有的时间。取代它们与reader.GetValues​​(myarray的)没有任何影响。
                    val0 = dataReader.GetValue(0);
                    参数val1 = dataReader.GetValue(1);
                    将val2 = dataReader.GetValue(2);
                }
            }
 

时的散装我目前正在与花在做的GetValue来电查询。难道让往返于数据库中的每个电话的GetValue?现在看来似乎是,这似乎非常低效。由于code的笔记,试图使用的GetValues​​()不会有所作为做在一杆。有没有一种方法来获得一次性的整行?更重要的是,有没有办法让整个结果集一炮打响?

感谢。

解决方案

 使用(连接)
    {
        SqlCommand的命令=新的SqlCommand(
          选择类别ID,类别名称FROM dbo.Categories; +
          选择雇员,名字从dbo.Employees
          连接);
        connection.Open();

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

        而(reader.HasRows)
        {
            Console.WriteLine(\ t {0} \ t {1},reader.GetName(0)
                reader.GetName(1));

            而(reader.Read())
            {
                Console.WriteLine(\ t {0} \ t {1},reader.GetInt32(0)
                    reader.GetString(1));
            }
            reader.NextResult();
        }
    }
 

In the following code, command is a DbCommand that has already been set up:

using( var dataReader = command.ExecuteReader() /*The actual execution of the query takes relatively little time.*/ ) {
                while( dataReader.Read() ) {
                    // These are what take all of the time. Replacing them all with reader.GetValues( myArray ) has no impact.
                    val0 = dataReader.GetValue( 0 );
                    val1 = dataReader.GetValue( 1 );
                    val2 = dataReader.GetValue( 2 );
                }
            }

The bulk of the time for the query I am currently working with is spent doing the GetValue calls. Is it making a round trip to the database for each GetValue call? It seems like it is, and this seems very inefficient. As the code notes, attempting to do it in one shot using GetValues() does not make a difference. Is there a way to get the entire row in one shot? Better yet, is there a way to get the entire result set in one shot?

Thanks.

解决方案

using (connection)
    {
        SqlCommand command = new SqlCommand(
          "SELECT CategoryID, CategoryName FROM dbo.Categories;" +
          "SELECT EmployeeID, LastName FROM dbo.Employees",
          connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        while (reader.HasRows)
        {
            Console.WriteLine("\t{0}\t{1}", reader.GetName(0),
                reader.GetName(1));

            while (reader.Read())
            {
                Console.WriteLine("\t{0}\t{1}", reader.GetInt32(0),
                    reader.GetString(1));
            }
            reader.NextResult();
        }
    }

这篇关于什么是从的DbDataReader读取数据的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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