C# 比較兩個datatable并找出修改差異的值
?
//要比較的兩個表 DataTable dtA; //修改前 DataTable dtB;//修改后 //調用方法 string isEquals = CompareDataTable(dtA, dtB); if (isEquals.Length != 0) { MessageBox.Show("修改內容:" + isEquals); }
?
/// <summary> /// 比較兩個DataTable內容是否相等,先是比數量,數量相等就比內容 /// </summary> /// <param name="dtA"></param> /// <param name="dtB"></param> /// <returns></returns> private string CompareDataTable(DataTable dtA, DataTable dtB) { StringBuilder strbulid = new StringBuilder(); //比內容 for (int i = 0; i < dtA.Rows.Count; i++) { for (int j = 0; j < dtA.Columns.Count; j++) { if (!dtA.Rows[i][j].Equals(dtB.Rows[i][j])) { //此處 不需要轉義,不然會報錯 strbulid.AppendFormat(@" " + "原值:{0} 新值:{1}", dtA.Rows[i][j].ToString(), dtB.Rows[i][j].ToString()); // return false; } } } return strbulid.ToString(); } }
?
本文摘自 :https://www.cnblogs.com/