DatTables

  • Thread starter Thread starter Steve Bezner
  • Start date Start date
S

Steve Bezner

I have a System.Data.DataTable in memory in which I would like to change the
value of a column for all rows. Is there a method to change all rows to a
specified value?

I know I can loop thru all the records (see below), but I would like to
update the datatable with 1 statement, perhaps an sql update statement? It
would also be useful if I can run an sql update statement on a datatable in
memory, instead of looping.

foreach (System.Data.DataRow row in System.Data.DataTable)
{
row["MyColumn"] = 0; // There has to be better way, then
to loop.....
}


Thanks,
Steve
 
Thanks.

So what your saying if I want to update all records based on a certain
condition, then I would have to loop thru each record and check for the
condition before changing. It would be nice if I could pass an sql update
statement. Looping and checking sounds expensive.

Thanks for your response,
Steve


Ignacio Machin said:
Hi Steve,

AFAIK that is not only the best way but the only :)

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Steve Bezner said:
I have a System.Data.DataTable in memory in which I would like to change the
value of a column for all rows. Is there a method to change all rows to a
specified value?

I know I can loop thru all the records (see below), but I would like to
update the datatable with 1 statement, perhaps an sql update statement? It
would also be useful if I can run an sql update statement on a datatable in
memory, instead of looping.

foreach (System.Data.DataRow row in System.Data.DataTable)
{
row["MyColumn"] = 0; // There has to be better way, then
to loop.....
}


Thanks,
Steve
 
Thanks. I didn't want to have to get into transactions, because these are
changes that may need to be rolled back. It would have been nice if MSFT
implemented more of the "engine" as they did with the select statement.




Ignacio Machin said:
Hi Steve,

Please remember that the classes of ADO.NET are mean to be an in memory
representation of a DB, they are not intended to work as a SQL engine.
If you are going to replace all the rows in a datatable, well it's better
that you call a SP in the database and replace the table with the new data


Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Steve Bezner said:
Thanks.

So what your saying if I want to update all records based on a certain
condition, then I would have to loop thru each record and check for the
condition before changing. It would be nice if I could pass an sql update
statement. Looping and checking sounds expensive.

Thanks for your response,
Steve
rows
to
a
specified value?

I know I can loop thru all the records (see below), but I would like to
update the datatable with 1 statement, perhaps an sql update statement?
It
would also be useful if I can run an sql update statement on a datatable
in
memory, instead of looping.

foreach (System.Data.DataRow row in System.Data.DataTable)
{
row["MyColumn"] = 0; // There has to be better
way,
then
to loop.....
}


Thanks,
Steve
 
Back
Top