Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two datasets in C#

i have two datasets and i need to compare these two datasets such that if ID does not exist in one table then i need to write insert Query else update query.

For Ex:

Id in One dataset        ID in second Dataset       
1                          1
2                          2
3                          4

I need to insert ID 3 to second dataset.

Here is my code for your reference:

if (ds.Tables[0].Rows.Count > 0 || clientDS.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                for (int j = 0; j < clientDS.Tables[0].Rows.Count; j++)
                {
                    if (ds.Tables[0].Rows[i]["Id"].ToString() == clientDS.Tables[0].Rows[j]["Id"].ToString())
                    {
                        client.GetSingleValue("update customers set Name='" + ds.Tables[0].Rows[i]["Name"].ToString() + "',ContactPerson= '" + ds.Tables[0].Rows[i]["ContactPerson"].ToString() + "',Address='" + ds.Tables[0].Rows[i]["Address"].ToString() + "',TinNo='" + ds.Tables[0].Rows[i]["TinNo"].ToString() + "',ContactNo='" + ds.Tables[0].Rows[i]["Contactno"].ToString() + "',Report=  '" + ds.Tables[0].Rows[i]["Report"].ToString() + "',Sync=0,Ids='" + ds.Tables[0].Rows[i]["Id"].ToString() + "' where id='" + ds.Tables[0].Rows[i]["Id"].ToString() + "' ");
                    }
                    else
                    {
                        client.GetSingleValue("insert into customers(id,Name,ContactPerson,Address,TinNo,ContactNo,Report,Sync,Ids) values('" + ds.Tables[0].Rows[i]["Id"].ToString() + "',  '" + ds.Tables[0].Rows[i]["Name"].ToString() + "','" + ds.Tables[0].Rows[i]["ContactPerson"].ToString() + "',  '" + ds.Tables[0].Rows[i]["Address"].ToString() + "',  '" + ds.Tables[0].Rows[i]["TinNo"].ToString() + "',  '" + ds.Tables[0].Rows[i]["Contactno"].ToString() + "',  '" + ds.Tables[0].Rows[i]["Report"].ToString() + "',0,'" + ds.Tables[0].Rows[i]["Id"].ToString() + "')");
                    }
                }
            }
        }  

Above code does not work. Pls rectify my issue.

Thanks

like image 611
GIRISH GMAIL Avatar asked Dec 14 '22 03:12

GIRISH GMAIL


2 Answers

Use the Merge method:

Dataset2.Merge(Dataset1);

This will insert into Dataset2 any records that are in Dataset1 but not already in Dataset2. Note: your original question suggests that you need to insert records or update matching records from Dataset1, but your comments appear to suggest that you don't actually need to do an update. The Merge method will only insert new records from Dataset1.

like image 110
MusiGenesis Avatar answered Dec 16 '22 18:12

MusiGenesis


DataSet data1
DataSet data2

data1.Merge(data2,true)

will merge data2 into data1 not overwrite rows with same primary key and add rows with non existing primary key in data1.

data1.Merge(data2,false)

will merge data2 into data1 overwriting all rows and add new rows

like image 44
Wyxlwiis Avatar answered Dec 16 '22 16:12

Wyxlwiis