How do you refresh a datatable with the newly updated data after you have pushed the data to the database?
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.
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.
Building the DataTable
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)));
}
Populating the DataTable
m_Table.Rows.Add(m_Table.Rows.Count + 1, resultid, tagtypeid, SqlStr(tagname), (isDel ? "1" : "0"), "", "");
Pushing to the Database
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;
}
Refreshing the DataTable
- Unsure what to attempt here. I have investigating using Linq to rebuild a SQL statement based upon the data in the DataTable.
- 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.
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.