B
Bryan
Hello,
I am trying to speed up the process for updating a datatable with new rows.
First of all I have 2 datatables, ServerTable and ClientTable, ServerTable
is from a remote datasource, it has new, and updated records in it.
ClientTable exists on the client. It needs the new/updated records from the
ServerTable. So I have created a loop that loops through all the records in
the ServerTable and for each record checks the primary key of the
ClientTable to and gets a like DataRow below:
object[] key = {drServer["UPC"].ToString(),drServer["ItemNum"].ToString()} ;
DataRow drClient = DataClass.dsGrids.Tables["ClientTable"].Rows.Find(key) ;
if it finds a datarow then it will change certain fields in drClient like
this:
drClient["field1"] = drServer["field1"] ;
drClient["field2"] = drServer["field2"] ;
if there is no match because drClient is null then I do an Row.Add(drServer)
on the ClientTable.
This is where it slows down considerably. It seems to take more than 3
minutes to add only 15,000 records to the ClientTable, then after the loop I
apply the update to the database (I am even using the new 2.0 batch update)
takes another 3 or more minutes for this to occur, although it does seem
when I change it to the batchupdate it ran a little quicker. Please see the
full example code below:
foreach(DataRow drServer in dtServer.Rows) // iterrate through the new
records, loop take more than 3 minutes for 15,000+ records
{
object[] key =
{drServer["UPC"].ToString(),drServer["ItemNum"].ToString()} ;
DataRow drClient =
DataClass.dsGrids.Tables["ClientTable"].Rows.Find(key) ;
if(drClient != null) //Update row
{
drClient["field1"] = drServer["field1"] ;
drClient["field2"] = drServer["field2"] ;
... 20+ more fields to update...
}
else //Add row
{
drClient = DataClass.dsGrids.Tables["ClientTable"].NewRow() ;
drClient["field1"] = drServer["field1"] ;
drClient["field2"] = drServer["field2"] ;
... 44 more fields to add...
DataClass.dsGrids.Tables["ClientTable"].Rows.Add(drN) ;
}
}
DataClass.daClientTable.UpdateCommand.UpdatedRowSource =
UpdateRowSource.None;
DataClass.daClientTable.InsertCommand.UpdatedRowSource =
UpdateRowSource.None;
DataClass.daClientTable.UpdateBatchSize = 20;
int Update =
DataClass.daAllSigns.Update(DataClass.dsGrids.Tables["ClientTable"]) ; //
Update take more than 3 minutes for Update
Anyone know what I am doing wrong? Surely it can be quicker....?
Any help would be great!
Thanks, Bryan
I am trying to speed up the process for updating a datatable with new rows.
First of all I have 2 datatables, ServerTable and ClientTable, ServerTable
is from a remote datasource, it has new, and updated records in it.
ClientTable exists on the client. It needs the new/updated records from the
ServerTable. So I have created a loop that loops through all the records in
the ServerTable and for each record checks the primary key of the
ClientTable to and gets a like DataRow below:
object[] key = {drServer["UPC"].ToString(),drServer["ItemNum"].ToString()} ;
DataRow drClient = DataClass.dsGrids.Tables["ClientTable"].Rows.Find(key) ;
if it finds a datarow then it will change certain fields in drClient like
this:
drClient["field1"] = drServer["field1"] ;
drClient["field2"] = drServer["field2"] ;
if there is no match because drClient is null then I do an Row.Add(drServer)
on the ClientTable.
This is where it slows down considerably. It seems to take more than 3
minutes to add only 15,000 records to the ClientTable, then after the loop I
apply the update to the database (I am even using the new 2.0 batch update)
takes another 3 or more minutes for this to occur, although it does seem
when I change it to the batchupdate it ran a little quicker. Please see the
full example code below:
foreach(DataRow drServer in dtServer.Rows) // iterrate through the new
records, loop take more than 3 minutes for 15,000+ records
{
object[] key =
{drServer["UPC"].ToString(),drServer["ItemNum"].ToString()} ;
DataRow drClient =
DataClass.dsGrids.Tables["ClientTable"].Rows.Find(key) ;
if(drClient != null) //Update row
{
drClient["field1"] = drServer["field1"] ;
drClient["field2"] = drServer["field2"] ;
... 20+ more fields to update...
}
else //Add row
{
drClient = DataClass.dsGrids.Tables["ClientTable"].NewRow() ;
drClient["field1"] = drServer["field1"] ;
drClient["field2"] = drServer["field2"] ;
... 44 more fields to add...
DataClass.dsGrids.Tables["ClientTable"].Rows.Add(drN) ;
}
}
DataClass.daClientTable.UpdateCommand.UpdatedRowSource =
UpdateRowSource.None;
DataClass.daClientTable.InsertCommand.UpdatedRowSource =
UpdateRowSource.None;
DataClass.daClientTable.UpdateBatchSize = 20;
int Update =
DataClass.daAllSigns.Update(DataClass.dsGrids.Tables["ClientTable"]) ; //
Update take more than 3 minutes for Update
Anyone know what I am doing wrong? Surely it can be quicker....?
Any help would be great!
Thanks, Bryan