title: Import excel file to sql server in asp.net with check existing record in table before import
body: <p>Sorry my Enghist is not good!</p>
  
  <p>How to check excel fie, if a row existing in table do'nt import this row</p>
  
  <p>My function work well for import (not check existing)</p>
  
  <pre><code>string excelConnectionString = "";
      string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
      string strFileType = System.IO.Path.GetExtension(fileuploadExcel.FileName).ToString().ToLower();
      fileuploadExcel.SaveAs(Server.MapPath("~/Import/" + strFileName + strFileType));
      string strNewPath = Server.MapPath("~/Import/" + strFileName + strFileType);
      //Connection String to Excel Workbook
      if (strFileType.Trim() == ".xls")
      {
          excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=Excel 8.0;Persist Security Info=False";
      }
      else if (strFileType.Trim() == ".xlsx")
      {
          excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=Excel 12.0;Persist Security Info=False";
      }
  
    //Create Connection to Excel work book
      OleDbConnection excelConnection =new OleDbConnection(excelConnectionString);
      //Create OleDbCommand to fetch data from Excel
      OleDbCommand cmd = new OleDbCommand("Select [account_id], [type],[first_name], [last_name], [title], [birthday], [tel],[phone], [email_1],[email_2], [remark],[del_if] from [Sheet1$]",excelConnection);
      excelConnection.Open();
      OleDbDataReader dReader;
      dReader = cmd.ExecuteReader();
      SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
      //Give your Destination table name
      sqlBulk.DestinationTableName = "contact";
  
      sqlBulk.WriteToServer(dReader);
  </code></pre>