title: Taking rows in chunks from datatable and inserting in database
body: <p>I have around 25k records in datatable. I already have update query written by previous developer which I can't change. What I am trying to do is as follows:   </p>
  
  <ol>
  <li>Take 1000 records at a time from datatable, records can vary from 1 to 25k.   </li>
  <li>Update query which is in string, replace IN('values here') clause of that with these 1000 records and fire query against database.</li>
  </ol>
  
  <p>Now, I know there are effecient ways to do it, like bulk insert by use of array binding , but I can't change present coding pattern due to restrictions.
  What I have tried to do:</p>
  
  <pre><code> if (dt.Rows.Count&gt;0)
              {
                  foreach (DataRow dr in dt.Rows)
                  {
  
                      reviewitemsend =reviewitemsend + dr["ItemID"].ToString()+ ',';
                      //If record count is 1000 , execute against database. 
                  }
  
              }
  </code></pre>
  
  <p>Now above approach is taking me nowwhere and am like struck. So another better aproach which I am thinking is below :</p>
  
  <pre><code>int TotalRecords = dt.rows.count;
  If (TotalRecords &lt;1000 &amp;&amp; TotalRecords &gt;0 )
   //Update existing query with this records by placing them in IN cluse and execute
  else
   {
          intLoopCounter = TotalRecords/1000; //Manage for extra records, as        counter will be whole number, so i will check modulus division also, if that is 0, means no need for extra counter, if that is non zero, intLoopCounter increment by 1
  for(int i= 0;i &lt; intLoopCounter; i++)
   {
      //Take thousand records at a time, unless last counter has less than 1000 records and execute against database
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Also, note update query is below : </p>
  
  <pre><code> string UpdateStatement = @" UPDATE Table
   SET column1=&lt;STATUS&gt;,                                                       
   column2= '&lt;NOTES&gt;',
   changed_by = '&lt;CHANGEDBY&gt;',                                        
   status= NULL,   
   WHERE ID IN  (&lt;IDS&gt;)";
  </code></pre>
  
  <p>In above update query, IDS are already replaced with all 25K record ID's, which will be shown to end user like that, internally only I have to execute it as separate chunks, So within  IN() cluase I need to insert 1k records at a time</p>