title: How to insert/Update 10000 rows in SQL Server using C# efficiently while comparing each row from database
body: <p>I have been given an Excel file from a customer. It has 4 columns: Id, name, place, date).</p>
  
  <p>I have a table in my database which stores these values. I have to check each row from Excel and compare its values to the database table. If a row already exists, then compare the date and update to latest date from Excel. If the row does not exist yet, insert a new row.</p>
  
  <p>I'm fetching each row and comparing its values using for a loop and updating database using insert/update statement by creating data table adapter. </p>
  
  <p>My problem is this operation is taking 4+ hours to update the data. Is there any efficient way to do this? I have searched a lot and found options like <code>SqlBulkCopy</code> but how will I compare each and every row from database?</p>
  
  <p>I'm using ASP.NET with C# and SQL Server.  </p>
  
  <p>Here's my code:   </p>
  
  <pre><code>for (var row = 2; row &lt;= workSheet.Dimension.End.Row; row++)
  {
      // Get data from excel 
      var Id = workSheet.Cells[row, 1].Text;
      var Name = workSheet.Cells[row, 2].Text;
      var Place = workSheet.Cells[row, 3].Text;
      var dateInExcel = workSheet.Cells[row, 4].Text;
  
      // check in database if ID exists in database then compare date and update database&gt; 
      if (ID.Rows.Count &lt;= 0) //no row exist in database 
      {
          // Insert row in the database using data table adapter's insert statement 
      }
      else if (Id.Rows.Count &gt; 0) //Id exists in database 
      {  
          if (Db.DateInDB &lt; (dateUpdate)) // compare dates 
          {
              // Update database with the new date using data table adapter Update statement.
          }
      }
  }
  </code></pre>