欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

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

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

查询得到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:

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

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

上一篇:Unity Shader实现翻书效果

栏    目:C#教程

下一篇:Unity3D UGUI实现翻书特效

本文标题:C# OleDbDataReader快速数据读取方式(3种)

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/4596.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有