title: How to refresh a C# DataTable with updated data due to adding to table with autoincrement?
body: <p>How do you refresh a datatable with the newly updated data after you have pushed the data to the database?</p>
  
  <p>I have an auto incrementing table that is being populated by a datatable/sqlbulkcopy combination.  I need the key information that is generated in the autoincrement down the line, and instead of building out a massive SQL statement, to requery the database, I'm convinced there has to be a better way to do it.</p>
  
  <p>The solution is has to be extremely quick.  I'm working with hundreds of thousands of records that need to be processed in a timely manner.</p>
  
  <p><strong>Building the DataTable</strong></p>
  
  <pre><code>    public void SetupTable()
      {
          m_Table = new DataTable("Table");
          m_Table.Columns.Add(new DataColumn("TagID", typeof(int)));
          m_Table.Columns.Add(new DataColumn("ResultID", typeof(int)));
          m_Table.Columns.Add(new DataColumn("TypeID", typeof(int)));
          m_Table.Columns.Add(new DataColumn("Name", typeof(string)));
          m_Table.Columns.Add(new DataColumn("bDel", typeof(int)));
          m_Table.Columns.Add(new DataColumn("Notes", typeof(string)));
          m_Table.Columns.Add(new DataColumn("Units", typeof(string)));
      }
  </code></pre>
  
  <p><strong>Populating the DataTable</strong></p>
  
  <pre><code> m_Table.Rows.Add(m_Table.Rows.Count + 1, resultid, tagtypeid, SqlStr(tagname), (isDel ? "1" : "0"), "", "");
  </code></pre>
  
  <p><strong>Pushing to the Database</strong></p>
  
  <pre><code>    public bool AddProcessedTable()
      {
          bool bSuccess = true;
          try
          {
              if (sqlConn.State != ConnectionState.Open) sqlConn.Open();
  
              SqlBulkCopy SqlCmd = new SqlBulkCopy(sqlConn);
              SqlCmd.BulkCopyTimeout = 600;
              SqlCmd.DestinationTableName = "Table";
              SqlCmd.WriteToServer(m_Table);
          }
          catch (Exception ex)
          {
              bSuccess = false;
              plog.LogMsg(String.Format("General Exception during DestWarehouse.AddProcessedTable(): {0}", ex.Message));
          }
          return bSuccess;
  
      }
  </code></pre>
  
  <p><strong>Refreshing the DataTable</strong></p>
  
  <ul>
  <li>Unsure what to attempt here.  I have investigating using Linq to rebuild a SQL statement based upon the data in the DataTable.</li>
  <li>I have investigate creating a DataTableReader from the DataTable and then using Load to reload the information.  However I did not see any connection information in the examples and seemed incorrect.</li>
  </ul>
  
  <p>I rarely post here to SO - so if I need to correct this post and add more details I definitely can but any help is extremely appreciated!  Thanks.</p>