How to update dataset.Tables["tab1"] from records in dataset.Tables["tab2"]?

A

AndiSHFR

Hi.

How do i "bulk update" data rows on a dataset table from another one
DataTable?

i have a dataset ds1 with a table "Groups" and a table "newgroupdata"
and i want to update the records in ds1.Tables["Groups"] from the data
in ds1.Tables["newgroupdata"].
Additional i want to insert groups wich are not already in "Groups"
too.

Is this possible.

At the moment i loop thru "newgroupdata" and do a

DataRow[] foundRows = ds1.Tables["Groups"].Select( "Group = '" +
findgroup + "'", "", DataViewRowState.Added );

if(0==foundRows.Length)
{
// Add group to "Groups"
}
else
{
// Update foundRows
}

running this on a table with multiple thousand group records is
horrable slow.
I fear this can only be done in a fast and efficient way using a
database server.
 
B

Bruce Wood

If you can, establish the Group column as the PrimaryKey for both
tables. Then you can use Find rather than Select, which is much faster.

If you set the PrimaryKey property for both tables, and if your table
columns are in the same order, you should be able to reduce your code
to something like this:

DataTable groups = ds1.Table["Groups"];
groups.BeginLoadData();
foreach (DataRow row in newgroupdata.Rows)
{
object[] values = row.ItemArray;
groups.LoadDataRow(values, false);
}
groups.EndLoadData();

Would that work for you?
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top