updating all row in one time in datatable

  • Thread starter Thread starter Som Nath Shukla
  • Start date Start date
S

Som Nath Shukla

i have data table containg 5000 rows
to update it i am using foreach loop an updating very row with same value.
buts its too slow.is there any way to update all the rows in single.
foreach(datarow dr in dt.rows)
{
dr[0]="abcd";
dr[1]="xyz";

}
instead of using for loop can i update hole column in single time.

thanks in advance
 
buts its too slow
Well, how long is it taking?
Is it perhaps bound to a UI at this point? Perhaps unbind it while you
update it... if you are confident of the new values, you might also
turn off constraint checking while updating
(DataSet.EnforceConstraints).

Marc
 
Oh, and if the purpose of this is to update a database, perhaps just
use SQL instead...

UPDATE TheTable
SET SomeColumn = "abcd",SomeOtherColumn = "xyz"
--WHERE etc
 
thanks alot
but i want only to update datatable not sql server database

It might help if you share the schema so we know what datatypes and sizes
you are working with.

Thank you,
Roger
 
You ignored my "how long" and "UI" points... for instance, the
following (over 10k rows) takes 35ms to insert 10k rows, and 35ms to
update 10k rows... so what are you doing differently? UI binding is my
best guess...

DataTable dt = new DataTable();
dt.Columns.Add("A", typeof(string));
dt.Columns.Add("B", typeof(string));
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 10000; i++)
{
dt.Rows.Add("abc", "def");
}
watch.Stop();
Console.WriteLine("Insert: {0}ms",
watch.ElapsedMilliseconds);
watch.Reset();
watch.Start();
foreach (DataRow row in dt.Rows)
{
row[0] = "ghi";
row[1] = "jkl";
}
watch.Stop();
Console.WriteLine("Update: {0}ms",
watch.ElapsedMilliseconds);
 
Back
Top