C# OleDbDataReader快速数据读取方式(3种)
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("方法一:
耗时:{0}s", time1.TotalSeconds));
Console.WriteLine(string.Format("方法二:
耗时:{0}s", time2.TotalSeconds));
Console.WriteLine(string.Format("方法三:
耗时:{0}s", time3.TotalSeconds));
Console.ReadKey();
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播