Fill DataSet from OleDbDataAdapter

冷暖自知 提交于 2019-12-12 16:41:01

问题


I'm using OleDbDataAdapter to read the content of an Excel sheet in a dataset. The Excel sheet consists of 20391 rows, the dataset reads the total number of rows when running it on my local machine, but when running the code on an IIS7.5 server it reads only the FIRST 12463!!

My connection string:

switch (strFileType.Trim())
       {
           case ".xls":
               connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath +
                            ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
               break;
           case ".xlsx":
               connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath +
                            ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
               break;
       }

var query = "SELECT * FROM [" + excelSheets[0] + "]";

//Create the connection object
var conn = new OleDbConnection(connString);
//Open connection
if (conn.State == ConnectionState.Closed) conn.Open();
//Create the command object
var cmd = new OleDbCommand(query, conn);
var da = new OleDbDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds);

Is there a way to divide da.Fill result in multiple datatables?


回答1:


I did what @dash suggested and used Excel Data Reader and it works correctly.

here is the code

        FileStream stream = File.Open(strNewPath , FileMode.Open, FileAccess.Read);            
        //1. Reading from a binary Excel file ('97-2003 format; *.xls)
        //IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
        //...
        //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);;                      
        excelReader.IsFirstRowAsColumnNames = true;
        DataSet result = excelReader.AsDataSet();


来源:https://stackoverflow.com/questions/15735184/fill-dataset-from-oledbdataadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!