Datatable updates taking too long...

  • Thread starter Thread starter VM
  • Start date Start date
V

VM

Why is the third line of code in the loop take so long? W/o that line, the
execution (35,000 loops) runs in 3 secs. With the line, it goes up to over 5
mins.

dataGrid_auditAddress.DataSource = null;
for(int i=0;i<table_rowNums.Rows.Count; i++)
{
iRowNum = Convert.ToInt32(table_rowNums.Rows["col_rowNums"]); //this
table has all row numbers that will be modified
DataRow[] row_1 = DT_tempTable.Select("col_row = " +
iRowNum.ToString());
row_1[0]["col_mark"] = MARKED; //line that takes too long
}
dataGrid_auditAddress.DataSource = DT_tempTable;

Is there a better way of doing it?
 
VM said:
Why is the third line of code in the loop take so long? W/o that line, the
execution (35,000 loops) runs in 3 secs. With the line, it goes up to over 5
mins.

dataGrid_auditAddress.DataSource = null;
for(int i=0;i<table_rowNums.Rows.Count; i++)
{
iRowNum = Convert.ToInt32(table_rowNums.Rows["col_rowNums"]); //this
table has all row numbers that will be modified
DataRow[] row_1 = DT_tempTable.Select("col_row = " +
iRowNum.ToString());
row_1[0]["col_mark"] = MARKED; //line that takes too long
}
dataGrid_auditAddress.DataSource = DT_tempTable;

Is there a better way of doing it?


I don't believe it's the third line which is taking that long - I
believe it's that having changed the contents of the table, the Select
takes a long time. I suggest you change to (say) building up an
ArrayList of all the rows you want to change, then changing them all in
one go.

(I'd also start using foreach for readability, btw.)
 
Do you mean that after I modify the table, the Select will take longer ?
Because if I comment the line that modifies the row, it all takes less than
4 secs.
Regarding your suggestion, I already have a table (an exact copy of the
original) that only includes all the records that are going to be modified.
Once I modifiy this temp table, how can I update the original table with the
rows in the temp table?

And you're right about the foreach, but I'm still having trouble
understanding it...

Thanks,
VM

Jon Skeet said:
VM said:
Why is the third line of code in the loop take so long? W/o that line, the
execution (35,000 loops) runs in 3 secs. With the line, it goes up to over 5
mins.

dataGrid_auditAddress.DataSource = null;
for(int i=0;i<table_rowNums.Rows.Count; i++)
{
iRowNum = Convert.ToInt32(table_rowNums.Rows["col_rowNums"]); //this
table has all row numbers that will be modified
DataRow[] row_1 = DT_tempTable.Select("col_row = " +
iRowNum.ToString());
row_1[0]["col_mark"] = MARKED; //line that takes too long
}
dataGrid_auditAddress.DataSource = DT_tempTable;

Is there a better way of doing it?


I don't believe it's the third line which is taking that long - I
believe it's that having changed the contents of the table, the Select
takes a long time. I suggest you change to (say) building up an
ArrayList of all the rows you want to change, then changing them all in
one go.

(I'd also start using foreach for readability, btw.)
 
VM said:
Do you mean that after I modify the table, the Select will take longer ?

Yes. There's a thread about it at the moment in the .adonet group.
Because if I comment the line that modifies the row, it all takes less than
4 secs.
Regarding your suggestion, I already have a table (an exact copy of the
original) that only includes all the records that are going to be modified.
Once I modifiy this temp table, how can I update the original table with the
rows in the temp table?

That probably wouldn't be terribly easy, although I'm sure it would be
doable. It shouldn't take up much memory to build up an ArrayList of
DataRows references though.
And you're right about the foreach, but I'm still having trouble
understanding it...

In this case it would be:

foreach (DataRow row in table_rowNums.Rows)
{
....
}

I would seriously think about abandoning ADO.NET until you've got the
basics of C# down thoroughly though - it'll make it much easier to get
to grips with ADO.NET itself.
 
Back
Top