2013-03-31 113 views
2

我使用OleDbDataAdapter讀取數據集中Excel工作表的內容。 Excel工作表包含20391行,數據集讀取在本地機器上運行時的總行數,但在IIS7.5服務器上運行代碼時,它只讀取FIRST 12463!從OleDbDataAdapter填充數據集

我的連接字符串:

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); 

有沒有辦法在多個數據表來劃分da.Fill結果呢?

+0

你認爲爲什麼傳播數據到多個數據表就能解決丟失數據的問題?我猜你在服務器上有一個不同的excel文件。 –

+0

你可以使用'try-catch'來檢查數據集填充過程中是否發生了什麼? – lexeRoy

+0

@SteveWellens我檢查了服務器上的文件和它的文件。我忘了說它只讀了FIRST 12463. –

回答

1

我做了什麼@dash建議和使用Excel Data Reader它工作正常。

這裏是代碼

 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();