2012-02-23 58 views
0

你好,我有下面的代碼由於某種原因不能正常工作:SQL批量複製不會上傳

我使用OLEDB來填補從通過FileUpload控件上載一個管道分隔文件中的數據集一個asp.net web應用程序。然後我拿到數據表,然後使用sql批量複製將數據複製到我已經在sql中設置的表中。

protected void btnUpload_Click(object sender, EventArgs e) 
{ 
    string filepath = fileUpload1.PostedFile.FileName; 
    PerformBulkCopy(GencoUpload(filepath)); 
} 

public static DataTable GencoUpload(string path) 
{ 
    string full = Path.GetFullPath(path); 
    string file = Path.GetFileName(full); 
    string dir = Path.GetDirectoryName(full); 

    string connString = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=\"" + dir + 
    "\\\";" + "Extended Properties=\"text;HDR=Yes;Format=Delimited(|)\";"; 

    string query = "SELECT * FROM " + file; 

    DataTable dt = new DataTable(); 

    OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString); 

    try 
    { 
     dAdapter.Fill(dt); 
    } 
    catch 
    { 
     // catch code 
    } 

    dAdapter.Dispose(); 
    return dt; 
} 

private void PerformBulkCopy(DataTable GencoInfo) 
{ 
    string conStr = ConfigurationManager.ConnectionStrings["EDI"].ConnectionString; 

    using (SqlBulkCopy bulkcopy = new SqlBulkCopy(conStr)) 
    { 
     bulkcopy.DestinationTableName = "dbo.GencoUploadTempTable"; 
     bulkcopy.WriteToServer(GencoInfo); 
    } 
} 

有人可以幫助我爲什麼這不是加載到我的sql數據庫?謝謝。

+0

沒有錯誤信息。按鈕點擊接受它,但沒有數據加載到數據庫中。它可能與管道(|)分隔符有關嗎?我聽說oledb不喜歡那個定界符。我不確定。 – Jeff 2012-02-23 15:13:47

+0

@ user1049984,通過使用IDE的代碼步驟。 dt和connString的值是什麼?我感覺你的數據源不正確,因爲dir只是一個目錄。 – 2012-02-23 15:17:01

+0

CONNSTRING值= CONNSTRING = 「供應商= Microsoft.Jet.OLEDB.4.0;數據源= \」 C:\\ \\的用戶我的名字\\桌面\\\ 「;擴展屬性= \」 文本; HDR =是;格式=分隔(|)\ 「;」 – Jeff 2012-02-23 15:24:59

回答

1

這應該做的工作...............

 public static DataTable columnNamessheet1(String excelFile) 
    { 
     string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFile + ";Extended Properties='Excel 12.0;HDR=yes'"; 
     string connectionstring ="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DebitCare;Data Source=SSDEV7-HP\\SQLEXPRESS"; 

     using (OleDbConnection conn = new OleDbConnection(connString)) 
     { 
      SqlConnection SqlConn1 = new SqlConnection(connectionstring); 
      SqlConn1.Open(); 
      OleDbCommand odc = new OleDbCommand(string.Format("Select LoanNumber,CustomerName,DateofBirth,MobileNo,SanctionDate,EmployerName FROM [BAJAJ DUMP$]"), conn); 
      conn.Open(); 
      OleDbDataReader reader = odc.ExecuteReader();    
      DataTable sheetSchema = reader.GetSchemaTable(); 
      SqlBulkCopy sqlbulk = new SqlBulkCopy(SqlConn1); 
      sqlbulk.DestinationTableName = "CUSTOMER_DETAILS"; 
      sqlbulk.ColumnMappings.Add("LoanNumber", "CUSTOMER_LOAN_NO"); 
      sqlbulk.ColumnMappings.Add("CustomerName", "CUSTOMER_ NAME"); 
      sqlbulk.ColumnMappings.Add("DateofBirth", "CUSTOMER_DOB"); 
      sqlbulk.ColumnMappings.Add("MobileNo", "CUSTOMER_MOBILE NUMBER"); 
      sqlbulk.ColumnMappings.Add("SanctionDate", "CUSTOMER_SANCTION DATE"); 
      sqlbulk.ColumnMappings.Add("EmployerName", "CUSTOMER_COMPANY NAME"); 
      sqlbulk.WriteToServer(reader); 
      conn.Close(); 
      return sheetSchema;    

     } 
    } 
0

試試這個,把一個斷點在catch:

string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;DataSource={0};Extended Properties=\"text;HDR=Yes;Format=Delimited(|)\";", dir); 

try 
{ 
    dAdapter.Fill(dt); 
} 
catch (Exception exc) 
{ 
    string myErrorMsg = exc.Message; 
} 
+0

仍然沒有工作。它說「Microsoft.jet.oledb.4.0」提供程序未在本地計算機上註冊。如果有幫助。 – Jeff 2012-02-23 16:07:33