开发者

OleDb - slow reading from Excel

I have table with several thousand rows, but even if I try to read 1000 rows (and 10 columns) it takes about 10 second to get result. I think it's too slow, but I can't find any problem.

Code for reading data:

string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties="Excel 8.0;HDR=Yes;"", _excelFilename);
using (OleDbConnection c = new OleDbConnection(connectionString)) {
    c.Open();
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Trend_Data$A1:J1000]", c);
    OleDbDataReader dbReader = dbCommand.ExecuteReader();

    while (dbReader.Read()) {
        object[] values = new object[10];
        dbReader.GetValues(values);

        //save values into internal structure - fast
        ...
    }

Stepping through the code I found that calling ExecuteReader function takes so long.

Do you have any 开发者_如何学JAVAideas, how to speed up code?


Try this OleDBAdapter Excel QA I posted via stack overflow.

I took my worksheet from the previous post and added columns (10 total) and rows (10,000 total) and packed it w/ data (the excel file is 1,770 KB). Using XP w/ 4 cpus, Visual Studio 2010, it takes less than 1 second.

Add this at the top:

Stopwatch queryWatch = new Stopwatch();
queryWatch.Start();

and this at the bottom:

Console.WriteLine("Elapsed Query time: {0}.{1} seconds.", queryWatch.ElapsedMilliseconds / 1000, queryWatch.ElapsedMilliseconds % 1000);    
Console.ReadLine();

Output:

Elapsed Query time: 0.623 seconds.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜