我想提出我的發現作爲答案,因爲行爲總是一致的。
我已經複製了你的代碼,並把一個按鈕點擊事件,只是改變了一下,以確保配置適配器和每個測試的連接。
// test.xls contains 26664 rows by 5 columns. Average 10 char for column, file size is 2448kb
// OS Windows 7 Ultimate 64 bit. CPU Intel Core2 Quad Q9550 2.83ghz
// 8gb ram and disk C is an 256gb SSD cruzer
private void button1_Click(object sender, EventArgs e)
{
string filename = "c:\\tmp\\test.xls";
Stopwatch sw1 = Stopwatch.StartNew();
var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; " +
"Extended Properties=Excel 12.0", filename);
using(var adapter = new OleDbDataAdapter("SELECT * FROM [roots$]", connectionString))
{
var ds = new DataSet();
adapter.Fill(ds, "roots");
sw1.Stop();
Console.WriteLine("Time taken for excel roots: {0} ms", sw1.Elapsed.TotalMilliseconds);
}
}
所以,這基本上就是你的代碼。該代碼在500ms內執行。但是.... 如果我在Excel 2010中保持文件test.xls打開,則執行時間跳至8000ms。
我自己也嘗試這種代碼的變化,但最終的結果是一樣的
private void button1_Click(object sender, EventArgs e)
{
string filename = "c:\\tmp\\test.xls";
Stopwatch sw1 = Stopwatch.StartNew();
var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; " +
"Extended Properties=Excel 12.0", filename);
using(OleDbConnection cn = new OleDbConnection(connectionString))
{
cn.Open();
using(var adapter = new OleDbDataAdapter("SELECT * FROM [roots$]", cn))
{
var ds = new DataSet();
adapter.Fill(ds, "roots");
sw1.Stop();
Console.WriteLine("Time taken for excel roots: {0} ms", sw1.Elapsed.TotalMilliseconds);
}
}
}
和,不,它不是的OleDbConnection的open(),始終是adapter.Fill()
數據集是「重」對象,更好地創建自己的類並使用datareader填充它的列表 – Boomer
我認爲大多數性能成本是連接時間(嘗試修改記錄集大小以查看流逝時間是否顯着增加) – Pynner
試試在連接完成後將StopWatch的開始移動到並查看方式這部分需要很多時間。但正如Boomer已經指出的那樣,嘗試使用OleDbCommand和OleDbDataReader而不是OleDbDataAdapter和DataSet,你也可能獲得相當的速度。 –