Compare two datasets in C #

I have two datasets, and I need to compare these two datasets, so if the identifier does not exist in the same table, then I need to write the insert query query Query else.

For Ex:

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

I need to insert ID 3 into the 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() + "')"); } } } } 

The above code does not work. Pls fix my problem.

thanks

+4
source share
4 answers

I think your error is matching the Id string with == . Try using Equals . I just use foreach and choose instead:

 foreach (DataRow row in ds.Tables[0].Rows) { string filter = string.Format("Id = '{0}'", row["Id"]); DataRow[] rows = clientDS.Tables[0].Select(filter); if (rows.length == 0) { // insert here } else { // update here } } 
+2
source

Use the Merge method:

 Dataset2.Merge(Dataset1); 

This inserts into Dataset2 any records that are in Dataset1 but not yet in Dataset2. Note. In the original question, you are prompted to insert records or update the corresponding records from Dataset1, but your comments seem to suggest that you really don't need to do the update. The Merge method will only insert new records from Dataset1.

+6
source
 DataSet data1 DataSet data2 data1.Merge(data2,true) 

combines data2 into data1 so as not to overwrite rows with the same primary key and add rows with a non-existent primary key in data1 .

 data1.Merge(data2,false) 

concatenates data2 to data1 , rewriting all rows and adding new rows

+2
source

Add both of these tables (DataTable instances) to the DataSet and add the relation.

DataSet ds = new DataSet (); ds.EnforceConstraints = false;

 DataTable dt1 = new DataTable("A"); DataTable dt2 = new DataTable("B"); dt1.Columns.Add("ID", typeof(int)); dt1.PrimaryKey = new DataColumn[] {dt1.Columns[0]}; dt1.Rows.Add(1); dt1.Rows.Add(2); dt1.Rows.Add(3); dt2.Columns.Add("ID", typeof(int)); dt2.Rows.Add(1); dt2.Rows.Add(2); dt2.Rows.Add(4); ds.Tables.Add(dt1); ds.Tables.Add(dt2); ds.Relations.Add("ID_REL", dt1.Columns[0], dt2.Columns[0]); foreach (DataRow r in ds.Tables["A"].Rows) { DataRow []child=r.GetChildRows("ID_REL"); Console.Write(r[0] + " " ); if (child.Length != 0) Console.WriteLine(child[0][0]); Console.WriteLine(); } 
+1
source

All Articles