0
private void InsertExcelRecords(string filepath)
{
    ExcelConn(filepath);            
    OleDbCommand Ecom = new OleDbCommand(Query, Econ);  
    Econ.Open();
    DataTable dtSheet = Econ.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

    foreach (DataRow dr in dtSheet.Rows)
    {
       string sheetName = dr["TABLE_NAME"].ToString();

       if (!sheetName.EndsWith("$"))
           continue;

       Query = string.Format("select * from  [" + sheetName + "]");       

       DataSet ds=new DataSet();  
       OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);  
       Econ.Close();  
       oda.Fill(ds);  
       DataTable Exceldt = ds.Tables[0];

       using (SqlConnection con = new SqlConnection(strConnString))
       {
           //connection();  
           //creating object of SqlBulkCopy    
           SqlBulkCopy objbulk = new SqlBulkCopy(con);  

           //assigning Destination table name    
           objbulk.DestinationTableName = "tblCompany"; 

           //Mapping Table column        
           objbulk.ColumnMappings.Add("Company", "CompanyName");
           objbulk.ColumnMappings.Add("Contact Telephone", "CompanyContactNumber");
           objbulk.ColumnMappings.Add("Website", "Website");
           objbulk.ColumnMappings.Add("Location", "Location");
           objbulk.ColumnMappings.Add("Targetted for", "TargettedFor");

           con.Open();  
           objbulk.WriteToServer(Exceldt);
           con.Close();
       }
    }
}

How to prevent duplicate data from Excel sheets when importing into SQL Server table?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
kumar
  • 1
  • Duplicates within the incoming data, or duplicates between the data and the destination table? – Tim Williams Nov 30 '16 at 05:47
  • Duplicates within the incoming data from excell – kumar Nov 30 '16 at 05:59
  • `select distinct` ? – Tim Williams Nov 30 '16 at 06:02
  • not working..i have a multiple field how can possible distinct...For Example, Previous table: id name address Excel data id name address 1 ram covai 2 mohan chennai 3.mohan chennai in this case ram only update into table. because mohan is repeated Note: i have more than 5000 record... – kumar Nov 30 '16 at 06:13
  • `Query = "select distinct * from [" + sheetName + "]";` That only deals sheet-by-sheet though... – Tim Williams Nov 30 '16 at 06:17

0 Answers0