2014-03-03 75 views
1

我試圖使用C#將ASP.NET從Excel導入200,000條記錄到SQL Server 2005。但是,當我嘗試瀏覽並導入文件時,只插入了幾條記錄(僅限於100條記錄)。我無法得到任何解決方案。我只能將少量記錄插入到SQL Server數據庫中

這裏是我的示例代碼

using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using System.Data.OleDb; 
using System.Data.Common; 
using System.Data.SqlClient; 



public partial class UploadExcel : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
///***when the button press, this code will execute*** 
    protected void Upload_Click(object sender, EventArgs e) 
    { 

     int count = 0;   
     try 
     { 
      string path = string.Concat(Server.MapPath("~/TempFiles/"), UploadExcel1.FileName); 
      //Save File as Temp then you can delete it if you want 
      UploadExcel1.SaveAs(path); 
      DataTable dtExcel = new DataTable(); 
      string SourceConstr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0",path); 
      OleDbConnection con1 = new OleDbConnection(SourceConstr); 
      string query = "Select * from [Sheet1$]"; 
      OleDbDataAdapter dataadapt = new OleDbDataAdapter(query,con1); 
      dataadapt.Fill(dtExcel); 
      ///connection string with in the database below 
      string sqlconn = System.Configuration.ConfigurationManager.ConnectionStrings["zena"].ToString(); 
      //all the excel file is stored in the data table here 
      for (int i = 0; i < dtExcel.Rows.Count; i++) 
      { 
       try 
       { 
        SqlConnection sconn = new SqlConnection(sqlconn); 
        sconn.Open(); 
        string insert_query12 = "insert into FCROperation (CallingNumber,OperatorId,OperatorName,CallReason) values("+dtExcel.Rows[i][0]+","+dtExcel.Rows[i][1]+",'"+dtExcel.Rows[i][2]+"','"+dtExcel.Rows[i][3]+"')"; 

        SqlCommand cmd = new SqlCommand(insert_query12,sconn); 
        count+=cmd.ExecuteNonQuery();    
       } 
       catch (Exception ex) 
       { 
        //continue;    
       } 
       //Label1.Text = ex.Message; 
      } 
      if (count == dtExcel.Rows.Count) 
      { 
       Label1.Text = "Success" + dtExcel.Rows.Count + count; 
      } 
      else 
      { 
       Label1.Text = "Failed" + dtExcel.Rows.Count + count; 
       //Label1 = dtExcel.Rows.Count; 
      } 
     } 
     catch (Exception ex) 
     { 
      Label1.Text = ex.Message; 
      //throw ex;   
     } 
     finally 
     {    
     } 
    } 
} 
+0

如果您的Excel單元格爲空,則必須應用DBNull.Value值。 –

+0

親愛的謝謝你的寶貴支持,我的excel充滿了記錄,並有可能在表中插入空值。 – Sapna

+0

您是如何確認僅插入100條記錄的?您是使用SELECT COUNT(*)還是使用右鍵單擊表並在編輯模式下打開?因爲在編輯模式下SSMS僅默認顯示100條記錄。 –

回答

0

這一個跟蹤導致異常的所有行替換您的循環代碼。而在循環後添加一個斷點檢查errors

Dictonary<int, Exception> errors = new Dictonary<int, Exception>(); 
for (int i = 0; i < dtExcel.Rows.Count; i++) 
    { 
     try 
     { 
      SqlConnection sconn = new SqlConnection(sqlconn); 
      sconn.Open(); 
      string insert_query12 = "insert into FCROperation (CallingNumber,OperatorId,OperatorName,CallReason) values("+dtExcel.Rows[i][0]+","+dtExcel.Rows[i][1]+",'"+dtExcel.Rows[i][2]+"','"+dtExcel.Rows[i][3]+"')"; 

      SqlCommand cmd = new SqlCommand(insert_query12,sconn); 
      count+=cmd.ExecuteNonQuery(); 

     } 
     catch (Exception ex) 
     { 
      errors.Add(i, ex); 
      //continue; 

     } 
     //Label1.Text = ex.Message; 
    } 
1

我想要推薦你,當你插入一行

using (SqlConnection sconn = new SqlConnection(sqlconn)) 
{ 
    sconn.Open(); 
    for (int i = 0; i < dtExcel.Rows.Count; i++) 
      { 
       try 
       {         
        string insert_query12 = "insert into FCROperation (CallingNumber,OperatorId,OperatorName,CallReason) values("+dtExcel.Rows[i][0]+","+dtExcel.Rows[i][1]+",'"+dtExcel.Rows[i][2]+"','"+dtExcel.Rows[i][3]+"')"; 
        SqlCommand cmd = new SqlCommand(insert_query12,sconn); 
        count+=cmd.ExecuteNonQuery();    
       } 
      catch (SqlException ex) 
      { 
       for (int i = 0; i < ex.Errors.Count; i++) 
       { 
       errorMessages.Append("Index #" + i + "\n" + 
        "Message: " + ex.Errors[i].Message + "\n" + 
        "LineNumber: " + ex.Errors[i].LineNumber + "\n" + 
        "Source: " + ex.Errors[i].Source + "\n" + 
        "Procedure: " + ex.Errors[i].Procedure + "\n"); 
       } 
       Console.WriteLine(errorMessages.ToString()); 
      } 
       catch (Exception ex) 
       { 
        //continue;    
       } 
       //Label1.Text = ex.Message; 
      } 
} 
+0

親愛的,我試着實現你的建議,但是要插入16個記錄。與excel中的記錄數目一樣精確。但只有16個值。 – Sapna

+0

我的回答是關於你使用DB的方法。請檢查,我更新了我的答案,以便您可以在執行INSERT期間看到sql中發生的所有異常。分析這些錯誤你可能會理解爲什麼只插入幾行。我也想知道,如果事務是回滾的,ExecuteNonQuery返回-1。所以你不會計算所有插入的行,而是計算插入的行的數量 - 回滾事務 – MikkaRin

0

看來,Excel的OLE打開連接只有一次,而不是每次都數據庫驅動程序僅查找電子表格的前100行。這可能是由於各種原因。例如,這裏是one user who found that the Excel print area affected the driver。我建議你將Excel數據剪切並粘貼到最新格式的新電子表格中,並查看情況是否改善。

相關問題