Array of table adapters?

  • Thread starter Thread starter mordock32
  • Start date Start date
M

mordock32

By the looks of it, the design of the table adapter makes it very hard
to deal with a bunch of them in a generic way. Let's say I have a typed
data set with 10 tables and relations between then. Am I supposed to
hard code every fill and update for each table? Ideally I would do
this:

for (int i = 0; i < adapters.Count; i++)
adapters.Update(tables);

or this:

for (int i = 0; i < adapters.Count; i++)
adapters.Fill(tables);

But that doesn't seem to be feasible. Am I just missing something
obvious?

Thanks!
 
Create your own Collection of TableAdapters. I have done this before, using
the System.Collections.ObjectModel.Collection<T> generic Collection.

--
HTH,

Kevin Spencer
Microsoft MVP
Short Order Coder
http://unclechutney.blogspot.com

What You Seek Is What You Get
 
Be carefull about the order of the updates...

If your DB has relations with foreign key constraints between tables, you
will have to proceed in this order :

1. Delete rows in child table that has been deleted in the child table
2. Delete rows in parent table that has been deleted in the parent table
3. Update/Insert rows in parent table that has been inserted of modified in
the parent table
4. Update/Insert rows in child table that has been inserted of modified in
the child table

Repeat steps 1 & 2 for all tables, then 3&4 for all tables...

You can use table.GetChanges(DataRowState.Deleted) and
table.GetChanges(DataRowState.Added | DataRowState.Modified) to get the
lines of the table that requires to passed to the update method of the
TableAdapter.

According this few guidelines, you can create an array (TableAdapter[]) or
collection (List<TableAdapter>) of TableAdapters. Then create a loop for
delete, and a "reverse" loop for insert/updates. The only prerequisite is
that the array or the collection need to have tableadapters in the right
order...

A last word, do not forget that the code snippets are not quite hard to
write (take a look at "snippy"). Building in a custom business object the
whole update without making it generic but writing all updates is not a huge
operation since the concepts are quite simple...

Hope that helps
Steve
 
Back
Top