473,385 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Update records using SqlDataAdapter

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[i];

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.

Sep 30 '07 #1
4 4814
Matteo,

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

Cor

Oct 1 '07 #2
Acceptchanges says that you have updated them and the changes can be
set as done in the datatables.
The problem is not in AcceptChanges because this method
only change the row state to Unchanged, but with the "for"
I set rows in correct status.

Read here:
http://www.devnewsgroups.net/group/m...opic16270.aspx

Thx! ;-)
Matteo Migliore.

Oct 1 '07 #3
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/micros...a346c8aad9c860

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
Oct 2 '07 #4
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.
Oct 2 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: skyjoker1 | last post by:
I think what I'm trying to do is something relatively simple. I have created a DataTable using the SQLDataAdapter.Fill method: Dim da As New SqlDataAdapter("SELECT * FROM Trans", cn) Dim ds As...
0
by: Hugo Lefevre | last post by:
Dear, I have on a form a datagrid (datagrid1) which is filled by a dataset created in the class Afwezigheid. A button "Alles" show the whole table in the datagrid and when someone wants to...
1
by: Wing | last post by:
Hi all, I have created 2 tables in sql database and join these 2 tables before assign the result to the dataset, and display the result in datagrid. Everything is fine up to this point. The...
4
by: Oscar Thornell | last post by:
Hi, I have a relativley large/complex typed dataset that contains 7-8 tables and some supporting relational tables (lookups) for many-to-many relations. A good exampel would be a dataset that...
4
by: steroche | last post by:
I would REALLY appreciate help please please please! Im sure it is probably blindingly obvious to most of you but I am totally in the dark here!I am lost - i thought i had finally figured out this...
2
by: Calvin KD | last post by:
Hi everyone, I know the solution to my problem is probably very simple but I just can't seem to figure out how to do it. My problem is, I have an XML file as a datastore, I read it into a dataset...
3
by: Antonio | last post by:
Can somebody tell my why the following procedure changes the data in the fields being updated to all the records in the database? private void updateRow(object source,...
2
by: Antonio | last post by:
Can someone tell me why the following procedure updates ALL the records in the database with the field being updated for one record? private void updateRow(object source,...
1
by: TonyJ | last post by:
Hello! I trying to update a table using sql server but no update will be done. I don't get any error at all. Here is an extract from the code. Does this seems to be right.
1
by: gaya12 | last post by:
I have a table called Student in a SQL databse.. I have to create a simple windows application in C# to Retrieve that table for the following functions.. 1.Add Data 2.Update Data 3.View Data ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.