2015-03-03 66 views
2

我正嘗試創建一個Excel數據上傳器,以便將Excel文件上傳到使用Winforms在C#中的SQL Server。我在上傳excel db文件到winforms C#中的sql server時出現錯誤#

bulkcopy.WriteToServer(dr);後,我得到這個錯誤:

Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.
The Connection for viewing your linked Microsoft Excel worksheet was lost.

我得到了有關注冊表編輯器here並試圖跟隨,但還是同樣的錯誤:

class Pass 
{ 
    static string _excelfilepath; 

    public static string excelfilepath { get { return _excelfilepath; } set { _excelfilepath = value; } } 

    public void importdatafromexcel() 
    { 
     //declare variables - edit these based on your particular situation 
     string ssqltable = "tStudent"; 
     // make sure your sheet name is correct, here sheet name is sheet1, so you can change your sheet name if have different 
     string myexceldataquery = "select idnum,fname,gname,mname,coacro,year,yrstat,sex,stat,telno,addr1,addr2,addr3,dbirth,mothname,fathname,civstat,religion,hssch from [masterlist$]"; 
     try 
     { 
      //create our connection strings 
      string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;data source=" + _excelfilepath + 
      ";extended properties=" + "\"excel 8.0;hdr=yes;\""; 
      string ssqlconnectionstring = @"Data Source=LYNDON-PC\LYNDON;Initial Catalog=trial;Persist Security Info=True;User ID=sa;Password=14323531"; 
      //execute a query to erase any previous data from our destination table 
      string sclearsql = "delete from " + ssqltable; 
      SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring); 
      SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn); 
      sqlconn.Open(); 
      sqlcmd.ExecuteNonQuery(); 
      sqlconn.Close(); 
      //series of commands to bulk copy data from the excel file into our sql table 
      OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring); 
      OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn); 
      oledbconn.Open(); 
      OleDbDataReader dr = oledbcmd.ExecuteReader(); 
      SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring); 
      bulkcopy.DestinationTableName = ssqltable; 
      while (dr.Read()) 
      { 
       bulkcopy.WriteToServer(dr); 
      } 

      oledbconn.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 
} 

什麼是錯誤的意思,並獲得代碼上傳我的Excel文件到我的數據庫在我的表中的SQL Server ...

+0

參觀此http://support.microsoft.com/kb/269495 – 2015-03-03 05:08:32

+0

您是否嘗試過這些步驟http://stackoverflow.com/questions/15129744/multiple-步驟-ole-db-operation-generated-errors-check-each-ole-db-status-value – 2015-03-03 05:09:15

+0

@Jenish Rabadiya,是的,我已經試過了,我已經把它連接在上面了.. – 2015-03-03 05:11:02

回答

1

我認爲你應該使用

bulkcopy.WriteToServer(dr); 

而是採用

while (dr.Read()) 
{ 
    bulkcopy.WriteToServer(dr); 
} 
+0

你能解釋一下爲什麼這會有幫助嗎? – 2015-03-06 22:11:07

+0

嗨。方法WriteToServer調用方法Read直到它返回false,操作被中止或發生錯誤。 https://msdn.microsoft.com/library/434atets(v=vs.110).aspx – 2015-03-06 22:25:04

相關問題