Update records using SqlDataAdapter

M

Matteo Migliore

Hi.

I writed a method that takes two DataTable with same schema
and return a third DataTable that:
- insert new rows from "compare" DataTable
- delete rows contained in "source" and not in "compare" DataTable
- update rows from "compare"

--------------------------------------
public static DataTable GetMergedTable(DataTable source, DataTable compare)
{
DataTable ret = new DataTable();
ret.Merge(source);
ret.Merge(compare);
ret.AcceptChanges();

for (int i = 0; i < ret.Rows.Count; i++) {
DataRow row = ret.Rows;

bool sourceFound = source.Rows.Find(row["id"]) != null;
bool compareFound = compare.Rows.Find(row["id"]) != null;

//Row added
if ((!sourceFound) && (compareFound))
row.SetAdded();

//Row modified
if ((sourceFound) && (compareFound))
row.SetModified();

//Row deleted
if ((sourceFound) && (!compareFound))
row.Delete();
}

return ret;
}
--------------------------------------

When I execute an update with SqlDataAdapter.Update
rows are inserted and deleted correctly, but never updated.

What is the problem?

Thx! ;-)
Matteo Migliore.
 
C

Cor Ligthert[MVP]

Matteo,

Acceptchanges says that you have updated them and the changes can be set as
done in the datatables.

Cor
 
C

Cor Ligthert [MVP]

I don't see in the message thread that it is solved.

By the way, this is just a copy of a Microsoft AdoNet newsgroup discussion.

http://groups.google.nl/group/micro...7b385ef555a/39a346c8aad9c860#39a346c8aad9c860

In my idea you should be able to do the GetChanges direct, which gives you
only a shallow copy and be able because of that to do the updates. A
acceptchanges does set all to unchanged, however after that it has updated
the changes on the old data, in fact it is SetChanges and AcceptChanges in
one. . For new and delete this is of course not the question.

Cor
 
M

Matteo Migliore

In my idea you should be able to do the GetChanges direct, which
gives you only a shallow copy and be able because of that to do the
updates. A acceptchanges does set all to unchanged, however after
that it has updated the changes on the old data, in fact it is
SetChanges and AcceptChanges in one. . For new and delete this is of
course not the question.

Thank you, but I don't understand how to solve this problem :).

I need to compare two DataTable, the first contains the old data
and the second contains the new data. How can I do?

Merge and GetChanges doesn't go beacuse the second DataTable
is readed from a XML file. So does not contain activity
on rows, they are all on Unchanged status, initially.

Thx! ;-)
Matteo Migliore.
 

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