欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# OleDbDataReader快速数据读取方式(3种)

程序员文章站 2023-10-31 12:48:40
查询得到oledbdatareader后,有三种方式支持数据读取,如下: //方法一**速度中等 oledbdatareader reader = command.exec...

查询得到oledbdatareader后,有三种方式支持数据读取,如下:

//方法一**速度中等
oledbdatareader reader = command.executereader();
while (reader.read())
{
  var t1 = reader[0]; 
}

//方法二**速度最慢
oledbdatareader reader = command.executereader();
while (reader.read())
{
  var t1 = reader["字段名"]; 
}

//方法三**速度最快
oledbdatareader reader = command.executereader();
while (reader.read())
{
  var t1 = reader.getvalue(0); 
}

关于速度的描述,下面我们来简单验证一下。

有一个数据库,数据库中的表test有105945行数据,设计一个循环读取来测试他们的读取速度。

测试源码如下:

static void main(string[] args)
    {
      string connstr = string.format("provider=microsoft.jet.oledb.4.0 ;data source={0};", "f:\\2.mdb");
      string s1 = "id";
      string s2 = "探测号";
      string s3 = "x";
      string s4 = "y";
      string s5 = "h";
      string sql = string.format("select {0},{1},{2},{3},{4} from 管点表", s1, s2, s3, s4, s5);
      timespan time1 = new timespan();
      timespan time2 = new timespan();
      timespan time3 = new timespan();

      int count = 50;

      //数据库中查询的表有105945行
      for (int i = 0; i < count; i++)
      {
        using (oledbconnection conn = new oledbconnection(connstr))
        {
          #region
          conn.open();
          oledbcommand command = conn.createcommand();
          command.commandtext = sql;
          oledbdatareader reader = command.executereader();
          datetime now = datetime.now;
          while (reader.read())
          {
            var t1 = reader[0]; var t2 = reader[1]; var t3 = reader[2]; var t4 = reader[3]; var t5 = reader[4];
          }
          time1 = time1 + (datetime.now - now);
          #endregion
        }
      }
      for (int i = 0; i < count; i++)
      {
        using (oledbconnection conn = new oledbconnection(connstr))
        {
          #region
          conn.open();
          
          oledbcommand command2 = conn.createcommand();
          command2.commandtext = sql;
          oledbdatareader reader = command2.executereader();
          datetime now = datetime.now;
          while (reader.read())
          {
            var t1 = reader[s1]; var t2 = reader[s2]; var t3 = reader[s3]; var t4 = reader[s4]; var t5 = reader[s5];
          }
          time2 = time2 + (datetime.now - now);
          #endregion
        }
      }
      for (int i = 0; i < count; i++)
      {
        using (oledbconnection conn = new oledbconnection(connstr))
        {
          #region
          conn.open();

          oledbcommand command3 = conn.createcommand();
          command3.commandtext = sql;
          oledbdatareader reader = command3.executereader();
          datetime now = datetime.now;
          while (reader.read())
          {
            var t1 = reader.getvalue(0); var t2 = reader.getvalue(1); var t3 = reader.getvalue(2); var t4 = reader.getvalue(3); var t5 = reader.getvalue(4);
          }
          time3 = time3 + (datetime.now - now);
          #endregion
        }
      }
      console.writeline(string.format("方法一:\r\n耗时:{0}s", time1.totalseconds));
      console.writeline(string.format("方法二:\r\n耗时:{0}s", time2.totalseconds));
      console.writeline(string.format("方法三:\r\n耗时:{0}s", time3.totalseconds)); 
      console.readkey();
    }

循环50次结果,即50*105945:

C# OleDbDataReader快速数据读取方式(3种)

C# OleDbDataReader快速数据读取方式(3种)

C# OleDbDataReader快速数据读取方式(3种)

100次循环结果:,即100*105945:

C# OleDbDataReader快速数据读取方式(3种)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。