2012-10-24 54 views
3

我使用SqlbulkCopy將excel數據導入到sql server,但問題是我想防止輸入任何重複的記錄。有沒有一種方法可以自動忽略或刪除重複項?在使用SqlBulkCopy時如何防止重複條目?

protected void Button1_Click(object sender, EventArgs e) 
{ 
    string strFileType = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower(); 
    string strFileName = FileUpload1.PostedFile.FileName.ToString(); 

    FileUpload1.SaveAs(Server.MapPath("~/Import/" + strFileName + strFileType)); 
    string strNewPath = Server.MapPath("~/Import/" + strFileName + strFileType); 

    string excelConnectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strNewPath + "; Extended Properties=Excel 8.0;"); 

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

     connection.Open(); 

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

      con.Open(); 
      DataTable dt1 = new DataTable(); 
      string s = "select count(*) from ExcelTable"; 
      string r = ""; 
      SqlCommand cmd1 = new SqlCommand(s, con); 

      try 
      { 
       SqlDataAdapter da1 = new SqlDataAdapter(cmd1); 
       da1.Fill(dt1); 
      } 
      catch 
      { 
      } 

      int RecordCount; 
      RecordCount = Convert.ToInt32(cmd1.ExecuteScalar()); 
      r = RecordCount.ToString(); 
      Label1.Text = r; 
      con.Close(); 
      int prv = Convert.ToInt32(r); 

      // Bulk Copy to SQL Server 
      using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString)) 
      { 
       bulkCopy.DestinationTableName = "ExcelTable"; 

       SqlBulkCopyColumnMapping mapping1 = new SqlBulkCopyColumnMapping("excelid", "id"); 
       SqlBulkCopyColumnMapping mapping2 = new SqlBulkCopyColumnMapping("exceldata", "data"); 

       bulkCopy.ColumnMappings.Add(mapping1); 
       bulkCopy.ColumnMappings.Add(mapping2); 

       bulkCopy.WriteToServer(dr); 
      } 

      con.Open(); 

      DataTable dt = new DataTable(); 
      s = "select count(*) from ExcelTable"; 
      r = ""; 
      SqlCommand cmd = new SqlCommand(s, con); 

      try 
      { 
       SqlDataAdapter da = new SqlDataAdapter(cmd); 
       da.Fill(dt); 
      } 
      catch 
      { 
      } 

      RecordCount = Convert.ToInt32(cmd.ExecuteScalar()); 
      r = RecordCount.ToString(); 
      Label1.Text = r; 
      con.Close(); 
      int ltr = Convert.ToInt32(r); 

      if (prv == ltr) 
      { 
       Label1.Text = "No records Added"; 
      } 
      else 
      { 
       Label1.Text = "Records Added Successfully !"; 
      } 
     } 
    } 
} 

這個問題可以通過創建一個觸發器來刪除重複項嗎?如果是的話如何?我是一個新手,我從來沒有創造過觸發器。

另一個問題是無論我嘗試我無法獲得列映射工作。當Excel表格和數據庫中的列名不同時,我無法上載數據。

回答

8

您可以創建INDEX

CREATE UNIQUE INDEX MyIndex ON ExcelTable(id, data) WITH IGNORE_DUP_KEY 

而當你將數據插入與批量分貝,所有重複的值不會插入。

+0

你不需要所有字段的索引? – Diego

+0

哦,他只導入id和data :)這是一個很好的解決方案,但它不會工作,如果重複的記錄可以真正存在於表上,只需在批量過程中忽略 – Diego

+0

@ user1557976在他的表中只有兩列。 – kmatyaszek

0

不,您可以在加載到db之前(例如運行DISTINCT操作)在您的dr對象上管理它,或者在數據庫上創建觸發器來檢查。該觸發器將減少批量插入的性能。

另一種選擇是將批量插入到一個臨時表,然後使用插入到目標表從你的臨時表中的SELECT DISTINCT

0

據我記得, 你可以在導入時過濾掉冗餘行從excel文件本身。

應在OleDbCommand構造函數中使用以下SQL查詢。

var command = new OleDbCommand("Select DISTINCT ID,Data FROM [Sheet1$]", connection);