2013-04-26 110 views
3

我想在.net中使用C#創建程序,用於上傳excel文件,讀取它並將記錄excel文件添加到來自excel數據的sql server數據庫中。 雖然這樣做,我有一個錯誤:無法找到可安裝的ISAM?找不到可安裝的ISAM

有人可以幫助我如何解決這個問題?

或者可以提供一些示例代碼以不同的方式做這種類型的任務?

protected void Button1_Click(object sender, EventArgs e) 
    { 
     String excelConnectionString1; 
     String fname = FileUpload1.PostedFile.FileName; 
     if (FileUpload1.PostedFile.FileName.EndsWith(".xls")) 
     { 
      String excelsheet; 
      FileUpload1.SaveAs(Server.MapPath("~/file/" + FileUpload1.FileName)); 

      if (FileUpload1.PostedFile.FileName.EndsWith(".xls")) 
      { 
       excelConnectionString1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/file/" + FileUpload1.FileName) + ";Extended Properties=Excel 8.0;HDR=Yes;"; 
       OleDbConnection myEcelConnection1 = new OleDbConnection(excelConnectionString1); 
       myEcelConnection1.Open(); 
       if (txtsheet.Text.Length == 0) 
       { 
        lblmsg.Text = "Please Write File Name"; 
       } 
       else 
       { 
        excelsheet = "[" + txtsheet.Text + "$" + "]"; 
        string sheet = "Select * from [" + txtsheet.Text + "$" + "]"; 
        OleDbCommand cmd1 = new OleDbCommand(sheet, myEcelConnection1); 
        cmd1.CommandType = CommandType.Text; 
        OleDbDataAdapter myAdapter1 = new OleDbDataAdapter(cmd1); 
        DataSet myDataSet1 = new DataSet(); 
        myAdapter1.Fill(myDataSet1); 
        int a = myDataSet1.Tables[0].Rows.Count - 1; 
        string name; 
        string dob; 
        for (int i = 0; i <= a; i++) 
        { 
         name = myDataSet1.Tables[0].Rows[i].ItemArray[0].ToString(); 
         dob = myDataSet1.Tables[0].Rows[i].ItemArray[1].ToString(); 
         SqlConnection con = new SqlConnection("Connection String for Sql Server"); 
         con.Open(); 
         SqlCommand command = new SqlCommand("Insert into info(name,dob)values(@valname,@valdob)", con); 
         command.Parameters.Add("@valname", SqlDbType.VarChar, 50).Value = name; 
         command.Parameters.Add("@valdob", SqlDbType.VarChar, 50).Value = dob; 
         command.CommandType = CommandType.Text; 
         SqlDataAdapter da = new SqlDataAdapter(command); 
         DataSet ds = new DataSet(); 
         da.Fill(ds); 
         con.Close(); 
        } 
       } 
      } 
     } 
    } 
} 

}

+0

你可以在這裏發佈的代碼?這樣人們可以看看可能是什麼問題。 – 2013-04-26 10:18:23

回答

6

有噴氣OLEDB驅動程序沒有64位版本,因此,如果您是在64位操作系統上運行這一點,你可能需要針對x86的在你的.NET應用程序,而不是任何CPU。

或者

當連接字符串的語法不正確,也會產生這個錯誤。使用多個擴展屬性參數時通常會發生這種情況下面是一個例子:

ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 
"Data Source=e:\My Documents\Book20.xls;Extended Properties=""Excel 12.0;HDR=NO;IMEX=1""" 

更改連接字符串,這樣

ConnectionString=" Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\NAVEEN KUMAR\DOTNET\windows\WindowsApplication1\WindowsApplication1\123.xls;Excel 12.0 Xml;HDR=YES" 
+0

我已經嘗試將配置更改爲x86,但只有任何cpu的選項,甚至我無法添加x86的新目標。 – Diwash 2013-04-26 10:08:15

+0

您是否檢查過我發佈的第二個解決方案? – 2013-04-26 10:09:18

+0

我檢查了連接字符串,並改變了很多次,但仍出現同樣的問題...當我運行代碼時,它顯示錯誤: – Diwash 2013-04-26 10:13:19

相關問題