2013-03-10 24 views
2

我正在寫一個簡單的程序來導入Excel表到我的數據庫工作,但我遇到的一個錯誤:導入Excel工作表與C#的SQL Server

找不到可安裝ISAM

我不確定這是什麼意思,經過幾個小時的搜索與這麼多不同的主題,我已經轉向SO。有很多關於Jet和ACE的討論,我不確定它們之間有什麼區別,但這裏是簡要的說明:我有一個叫做test或者test1的excel文件,我只想導入文件中的第一張。這裏是我的源代碼到目前爲止:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.OleDb; 
using System.Data.Common; 
using System.Data.SqlClient; 

namespace WindowsFormsApplication2 
{ 

    public partial class Form1 : Form 
    { 
     string filePath = null; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     //Method to check database connection 
     private void button1_Click(object sender, EventArgs e) 
     { 
      string connetionString = null; 
      SqlConnection cnn; 
      connetionString = "Data Source=Zach-PC;Initial Catalog=master;Integrated Security=SSPI;"; 
      cnn = new SqlConnection(connetionString); 
      try 
      { 
       cnn.Open(); 
       MessageBox.Show("Connection Open ! "); 
       cnn.Close(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Can not open connection ! "); 
      } 
     } 

     //Method to select a file 
     private void button2_Click(object sender, EventArgs e) 
     { 
      string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/Zach/Documents/test1.xls;Extended Properties=Excel 12.0,HDR=Yes;IMEX=1"; 


      // Create Connection to Excel Workbook 
      using (OleDbConnection connection = 
         new OleDbConnection(excelConnectionString)) 
      { 
       OleDbCommand command = new OleDbCommand 
         ("Select * FROM [Sheet1$]", connection); 

       connection.Open(); //HERE IS WHERE THE ERROR IS 

       // Create DbDataReader to Data Worksheet 
       using (DbDataReader dr = command.ExecuteReader()) 
       { 
        // SQL Server Connection String 
        string sqlConnectionString = "Data Source=Zach-PC;Initial Catalog=master;Integrated Security=True"; 

        // Bulk Copy to SQL Server 
        using (SqlBulkCopy bulkCopy = 
           new SqlBulkCopy(sqlConnectionString)) 
        { 
         bulkCopy.DestinationTableName = "Table"; 
         bulkCopy.WriteToServer(dr); 
         MessageBox.Show("Data Exoprted To Sql Server Succefully"); 
        } 
       } 

      } 
     } 
    } 
} 

我在接近這個在正確的莊園嗎?

+0

嘗試分號:'Excel的12.0; HDR =是; IMEX = 1' – shibormot 2013-03-10 21:54:01

+0

同樣的錯誤在這裏,我完全在這裏輸了。 – 2013-03-10 22:04:03

+1

'......「;擴展屬性= \」Excel 12.0; HDR =是; IMEX = 1 \「;」'? – shibormot 2013-03-10 22:10:10

回答

4

你需要用連接字符串的Extended Properties一部分引號:Excel的12.0後

//                                 here      and here 
// -->                                v       v 
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/Zach/Documents/test1.xls;Extended Properties=""Excel 12.0,HDR=Yes;IMEX=1"""; 
+0

這解決了這個問題謝謝!現在看來我有一個問題訪問我的數據庫中的表哈哈。 – 2013-03-10 23:09:35

0

Office oledb驅動程序可能未安裝在您的計算機上。你應該從microsoft website下載它。安裝後,您的代碼應該運行。

+0

我剛剛安裝了它,但我在運行代碼時收到同樣的錯誤。 – 2013-03-10 21:34:05

0

如果你正在閱讀office 2007(或更新的)excel文件,那麼我會建議使用開源庫Epplus來讀取excel文件。它純粹是.NET庫,你不會依賴於oledb驅動程序。

epplus

EPPlus是一個.NET庫,讀取和寫入Excel中使用開放式的Office XML格式(XLSX)2007/2010文件。

你可以使用這個庫輕鬆地將excel文件讀入數據表。看看這個線程

How convert stream excel file to datatable C#?

+0

我不得不在一會兒看這個,感到沮喪,不得不休息一下。如果這解決了我的問題,我會更新。 – 2013-03-10 22:17:46