Update database with a dataset?

Q

Q. John Chen

All,

I am using VS2005.

I have a dataset MyDataSet with several DataTableAdapters defined (say
CompanyDataTable, DepartmentDataTable)
After I modified the data in the data tables of the dataset, I want
to put the change back to the database. So, I am doing the following:
MyDataSetTableAdapters.CompanyTableAdapter adapter = new ..
adapter.Update(myDataSet.CompanyDataTable);

MyDataSetTableAdapters.DepartmentTableAdapter adaperDept = new ..
adapterDept.Update(myDataSet.DepartmentDataTable)

The above worked but I have quite a few tables. Is there a method that
I can do and update all at once instead one table at a time?

TIA,

John
 
U

Usenet User

All,

I am using VS2005.

I have a dataset MyDataSet with several DataTableAdapters defined (say
CompanyDataTable, DepartmentDataTable)
After I modified the data in the data tables of the dataset, I want
to put the change back to the database. So, I am doing the following:
MyDataSetTableAdapters.CompanyTableAdapter adapter = new ..
adapter.Update(myDataSet.CompanyDataTable);

MyDataSetTableAdapters.DepartmentTableAdapter adaperDept = new ..
adapterDept.Update(myDataSet.DepartmentDataTable)

The above worked but I have quite a few tables. Is there a method that
I can do and update all at once instead one table at a time?

TIA,

John

Not really. Ultimately, updating each table will result in a separate
set of SQL UPDATE statements.

You may simplify your coding if you create a reusable method that will
look for updates in a dataset and update each table. For example:

public static void UpdateDataSet( DataSet data )
{
foreach( DataTable table in data.Tables )
{
// If table has changes, update the data in the DB
...
}
}
 

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