2015-02-24 25 views
0

類型「System.Data.OleDb.OleDbException」未處理的異常出現在system.data.dll我得到拋出異常,當我嘗試運行此代碼。(進口到SQL數據庫)

更多信息:外部表格不是預期的格式。

if (comboBox1.SelectedItem.ToString() == "Comissioned Sites") 
      { 
       string pathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtFilePath.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";"; 

       // Create Connection to Excel Workbook 
       using (OleDbConnection connection = new OleDbConnection(pathConn)) 
       { 
        OleDbCommand command = new OleDbCommand("Select * FROM [" + txtSheet.Text + "$]", connection); 

        connection.Open(); 

        // Create DbDataReader to Data Worksheet 
        using (OleDbDataReader dr = command.ExecuteReader()) 
        { 
         // Bulk Copy to SQL Server 
         using (SqlBulkCopy bulkCopy = new SqlBulkCopy(con)) 
         { 
          con.Open(); 
          bulkCopy.DestinationTableName = "tblView"; 
          bulkCopy.WriteToServer(dr); 
          con.Close(); 
         } 
        } 
       } 
       MessageBox.Show("File Imported to Database Successfully"); 
      } 

誰能幫助我

+0

如果您使用Excel 2007,http://stackoverflow.com/questions/1139390/excel-external-table-is-not-in-the-expected-format – 2015-02-24 13:05:19

+0

不,我使用excel 2013. – 2015-02-24 13:09:21

回答

0

嘗試在你的Excel文件中修改列類型。

右鍵單擊單元格,然後單擊格式化單元格 - >選擇日期作爲類別。

由於Oledb會將值作爲日期類型讀取,因此應該立即開始工作。

+0

仍然沒有工作..任何其他選項? – 2015-02-24 13:41:59

+0

試試這個[link] http://stackoverflow.com/questions/4538321/reading-datetime-value-from-excel-sheet – 2015-02-24 13:48:08

相關問題