C# OleDbDataReader快速數(shù)據(jù)讀取方式(3種)
查詢得到OleDbDataReader后,有三種方式支持?jǐn)?shù)據(jù)讀取,如下:
//方法一**速度中等
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);
}
關(guān)于速度的描述,下面我們來(lái)簡(jiǎn)單驗(yàn)證一下。
有一個(gè)數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)中的表TEST有105945行數(shù)據(jù),設(shè)計(jì)一個(gè)循環(huán)讀取來(lái)測(cè)試他們的讀取速度。
測(cè)試源碼如下:
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 = "探測(cè)號(hào)";
string s3 = "X";
string s4 = "Y";
string s5 = "H";
string sql = string.Format("SELECT {0},{1},{2},{3},{4} FROM 管點(diǎn)表", s1, s2, s3, s4, s5);
TimeSpan time1 = new TimeSpan();
TimeSpan time2 = new TimeSpan();
TimeSpan time3 = new TimeSpan();
int count = 50;
//數(shù)據(jù)庫(kù)中查詢的表有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耗時(shí):{0}s", time1.TotalSeconds));
Console.WriteLine(string.Format("方法二:\r\n耗時(shí):{0}s", time2.TotalSeconds));
Console.WriteLine(string.Format("方法三:\r\n耗時(shí):{0}s", time3.TotalSeconds));
Console.ReadKey();
}
循環(huán)50次結(jié)果,即50*105945:



100次循環(huán)結(jié)果:,即100*105945:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C# 使用multipart form-data方式post數(shù)據(jù)到服務(wù)器
- 詳解C# 泛型中的數(shù)據(jù)類(lèi)型判定與轉(zhuǎn)換
- C#導(dǎo)出數(shù)據(jù)到excel如何提升性能
- 詳解C#數(shù)據(jù)類(lèi)型及其轉(zhuǎn)換
- 詳解如何獲取C#類(lèi)中發(fā)生數(shù)據(jù)變化的屬性信息
- c# 利用易福門(mén)振動(dòng)模塊VSE002采集振動(dòng)數(shù)據(jù)的方法
- C#使用TensorFlow.NET訓(xùn)練自己的數(shù)據(jù)集的方法
- 基于C# 寫(xiě)一個(gè) Redis 數(shù)據(jù)同步小工具
- C#連接SQL Server數(shù)據(jù)庫(kù)的實(shí)例講解
- c# 數(shù)據(jù)標(biāo)注與數(shù)據(jù)校驗(yàn)
相關(guān)文章
Unity實(shí)現(xiàn)UI光暈效果(發(fā)光效果)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)UI光暈效果,發(fā)光效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
C#實(shí)例化和靜態(tài)類(lèi)對(duì)象調(diào)用對(duì)比
這篇文章主要介紹了C#實(shí)例化和靜態(tài)類(lèi)對(duì)象調(diào)用對(duì)比,什么時(shí)候用實(shí)例化對(duì)象,什么時(shí)候用靜態(tài)類(lèi)對(duì)象,內(nèi)存和生命周期又是如何,框架本身的回收機(jī)制是什么,下文詳細(xì)解說(shuō)需要的小伙伴可以參考一下2022-04-04
C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問(wèn)題
這篇文章主要介紹了C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問(wèn)題 ,文中給大家列舉通過(guò)兩種方法來(lái)判斷,需要的朋友可以參考下2018-10-10
c#實(shí)現(xiàn)51單片機(jī)頻率計(jì)的代碼分享(數(shù)字頻率計(jì)設(shè)計(jì))
c#實(shí)現(xiàn)51單片機(jī)頻率計(jì)的代碼分享,大家參考使用吧2013-12-12
C#中Dictionary泛型集合7種常見(jiàn)的用法
本文主要介紹了Dictionary集合的7種最基礎(chǔ)的用法,包括創(chuàng)建、添加、查找、遍歷、刪除等方法,程序都是由簡(jiǎn)入繁,希望能通過(guò)閱讀簡(jiǎn)單的示例,給大家一些啟發(fā)。2016-03-03
c#動(dòng)態(tài)編譯執(zhí)行對(duì)象方法示例 運(yùn)用映射機(jī)制創(chuàng)建對(duì)象
本示例核心技術(shù)是運(yùn)用.NET動(dòng)態(tài)編譯技術(shù)+.NET映射技術(shù),把一個(gè)代碼塊中的代碼,動(dòng)態(tài)編譯成程序集后,在運(yùn)用映射機(jī)制,創(chuàng)建對(duì)象示例,調(diào)用對(duì)象方法2014-01-01
C# Socket通信的實(shí)現(xiàn)(同時(shí)監(jiān)聽(tīng)多客戶端)
這篇文章主要介紹了C# Socket通信的實(shí)現(xiàn)(同時(shí)監(jiān)聽(tīng)多客戶端),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
c# wpf使用GMap.NET類(lèi)庫(kù),實(shí)現(xiàn)地圖軌跡回放
這篇文章主要介紹了c# wpf使用GMap.NET類(lèi)庫(kù),實(shí)現(xiàn)地圖軌跡回放的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03

