tag:blogger.com,1999:blog-9045620729834119138.post2996245381718195611..comments2023-05-27T21:41:09.273-07:00Comments on Microsoft .Net Solutions: compare two datatablesGovindhttp://www.blogger.com/profile/10000613501242892857noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-9045620729834119138.post-4068204622731622952019-04-03T17:53:54.920-07:002019-04-03T17:53:54.920-07:00public DataTable CompareDataTables(DataTable first...public DataTable CompareDataTables(DataTable first, DataTable second)<br />{<br />first.TableName = "FirstTable";<br />second.TableName = "SecondTable";<br /><br />//Create Empty Table<br />DataTable table = new DataTable("Difference");<br /><br />try<br />{<br />//Must use a Dataset to make use of a DataRelation object<br />using (DataSet ds = new DataSet())<br />{<br />//Add tables<br />ds.Tables.AddRange(new DataTable[] { first.Copy(), second.Copy() });<br /><br />//Get Columns for DataRelation<br />DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];<br /><br />for (int i = 0; i < firstcolumns.Length; i++)<br />{<br />firstcolumns[i] = ds.Tables[0].Columns[i];<br />}<br /><br />DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];<br /><br />for (int i = 0; i < secondcolumns.Length; i++)<br />{<br />secondcolumns[i] = ds.Tables[1].Columns[i];<br />}<br /><br />//Create DataRelation<br />DataRelation r = new DataRelation(string.Empty, firstcolumns, secondcolumns, false);<br /><br />ds.Relations.Add(r);<br /><br />//Create columns for return table<br />for (int i = 0; i < first.Columns.Count; i++)<br />{<br />table.Columns.Add(first.Columns[i].ColumnName, first.Columns[i].DataType);<br />}<br /><br />//If First Row not in Second, Add to return table.<br />table.BeginLoadData();<br /><br />foreach (DataRow parentrow in ds.Tables[0].Rows)<br />{<br />DataRow[] childrows = parentrow.GetChildRows(r);<br />if (childrows == null || childrows.Length == 0)<br />table.LoadDataRow(parentrow.ItemArray, true);<br />}<br /><br />table.EndLoadData();<br /><br />}<br />}<br /><br /><br />return table;<br />}Anonymoushttps://www.blogger.com/profile/07221273346964551388noreply@blogger.comtag:blogger.com,1999:blog-9045620729834119138.post-74240340598926649802019-04-03T17:52:28.307-07:002019-04-03T17:52:28.307-07:00public DataTable CompareDataTables(DataTable first... public DataTable CompareDataTables(DataTable first, DataTable second)<br /> {<br /> first.TableName = "FirstTable";<br /> second.TableName = "SecondTable";<br /><br /> //Create Empty Table<br /> DataTable table = new DataTable("Difference");<br /><br /> try<br /> {<br /> //Must use a Dataset to make use of a DataRelation object<br /> using (DataSet ds = new DataSet())<br /> {<br /> //Add tables<br /> ds.Tables.AddRange(new DataTable[] { first.Copy(), second.Copy() });<br /><br /> //Get Columns for DataRelation<br /> DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];<br /><br /> for (int i = 0; i < firstcolumns.Length; i++)<br /> {<br /> firstcolumns[i] = ds.Tables[0].Columns[i];<br /> }<br /><br /> DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];<br /><br /> for (int i = 0; i < secondcolumns.Length; i++)<br /> {<br /> secondcolumns[i] = ds.Tables[1].Columns[i];<br /> }<br /><br /> //Create DataRelation<br /> DataRelation r = new DataRelation(string.Empty, firstcolumns, secondcolumns, false);<br /><br /> ds.Relations.Add(r);<br /><br /> //Create columns for return table<br /> for (int i = 0; i < first.Columns.Count; i++)<br /> {<br /> table.Columns.Add(first.Columns[i].ColumnName, first.Columns[i].DataType);<br /> }<br /><br /> //If First Row not in Second, Add to return table.<br /> table.BeginLoadData();<br /><br /> foreach (DataRow parentrow in ds.Tables[0].Rows)<br /> {<br /> DataRow[] childrows = parentrow.GetChildRows(r);<br /> if (childrows == null || childrows.Length == 0)<br /> table.LoadDataRow(parentrow.ItemArray, true);<br /> }<br /><br /> table.EndLoadData();<br /><br /> }<br /> }<br /><br /><br /> return table;<br /> }Anonymoushttps://www.blogger.com/profile/07221273346964551388noreply@blogger.comtag:blogger.com,1999:blog-9045620729834119138.post-48170231697416391242018-08-07T00:10:09.726-07:002018-08-07T00:10:09.726-07:00where are you returning the table?where are you returning the table?Anonymoushttps://www.blogger.com/profile/05459093131454480031noreply@blogger.com