c# - 为什么 OleDbDataAdapter.Fill() 非常慢?

标签 c# asp.net excel oledbdataadapter

我正在尝试构建一个将读取 excel 文件(.xlsx、.xls)的应用程序。不幸的是,OleDbDataAdapter.Fill() 的性能非常糟糕。我最多需要 2 分钟才能从文件中读取一条记录。

有关该文件的更多信息:

  • 大小 - 257MB
  • 列 - 104
  • 行 - 1 000 000

  • 我目前用来读取文件的代码:
        string conStr = string.Empty;
        string strQuery = string.Empty;
        switch (extension)
        {
            case ".xls": //Excel 97-03
                conStr = @"Provider=Microsoft.Jet.OleDb.12.0;Data Source=" + file_source + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;';";
                strQuery = "SELECT " + col_no + " * FROM [" + workbook + "] ";
                break;
    
            case ".xlsx": //Excel 07
                //connection string to connect to the xlsx file
                conStr = @"Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + file_source + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1;';";
                strQuery = "SELECT " + col_no + " * FROM [" + workbook + "] ";
                break;
        }
        DataTable tbl = new DataTable();
        OleDbDataAdapter ada = new OleDbDataAdapter(strQuery, conStr);
        ada.Fill(tbl);
        ada.Dispose();
    
        return tbl;
    

    您的帮助将不胜感激!

    谢谢!

    最佳答案

    将大型数据集存储在 Excel 文件中是一个坏主意。我认为在 2 分钟内阅读 250MB Excel 文件是可以预料的。

    我建议切换到任何数据库解决方案。但是如果你不能改变你的数据存储,你可以尝试使用 Excel COM 对象(虽然它需要在服务器机器上安装 Excel)。 Here是泰迪·加兰的走道。

    关于c# - 为什么 OleDbDataAdapter.Fill() 非常慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24865344/

    相关文章:

    c# - 安装 Visual Web Developer 2010 时出现问题?

    asp.net - 我在哪里可以获得 asp.net 身份的 rtm Controller 模板?

    excel - 使用 Grails Export 插件在单个 Excel 文档中生成多个工作表

    sql - VBA 在尝试引用 SQL 数据库时不断出错

    vba - 即使 Excel 中存在多条记录,CopyFromRecordset 也仅复制并粘贴前一行

    c# - AdMob - iOS 实现 RewardBasedVideoAd

    c# - 多线程读取xml文件

    具有公共(public) 'Instance' 字段而不是属性的 C# 单例

    c# - ASP.NET MVC 路由与特定操作

    c# - 使用 itextsharp 从 pdf 中获取文本段落