How to update dataset with multiple tables in C#?

A

afunguy

I have the problem that when I use update() function to save multiple
tables back to database. There are two tables, Parent and Child. And
Child.ParentID is referenced to Parent.ID.Before the dataset saved
back to database, the parentID in child table is consistent with the
ID in parent table. But after saveing the dataset back to database,
the ID in parent table is automatically increment one and this causes
the Parent.ID and Child.ParentID are not consistent. Is there any
easier way to update dataset simply by using update function in C#? I
want to avoid the tedious way of writing lots of codes manually to
save multiple tables back to database. Thanks

For exampe, I have a strong type dataset TestDataSet, and 2 tables:
parent and child inside TestDataSet.


TestDataSet ds = new TestDataSet();
test.testParentRow parent = ds.testParent.NewtestParentRow();
parent.Animal = "dog"
ds.testParent.AddtestParentRow(tp);

test.testChildRow child= ds.testChild.NewtestChildRow();
child.Name = "baby";
child.testParentRow = tp;
ds.testChild.AddtestChildRow(child);

dataGrid1 = ds;

Update(ds);



This is dataGrid1 which shows the correct relationship.

ID Animal
1 dog

ID Name parentID
1 my dog 1


But after it execute Update(ds). The data value in database is wrong
as followed:

// parent table in database

ID Animal
----------------
1 Cat
2 human
3 bird
4 dog //after updating


//child table in database

ID Name parentID
---------------------------------
1 meou 1
2 African 2
3 flying bird 3
4 my dog 1 // should be 4, not 1, whichh is WRONG!!

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 

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