C# 比較兩個(gè)datatable并找出修改差異的值
?
//要比較的兩個(gè)表 DataTable dtA; //修改前 DataTable dtB;//修改后 //調(diào)用方法 string isEquals = CompareDataTable(dtA, dtB); if (isEquals.Length != 0) { MessageBox.Show("修改內(nèi)容:" + isEquals); }
?
/// <summary> /// 比較兩個(gè)DataTable內(nèi)容是否相等,先是比數(shù)量,數(shù)量相等就比內(nèi)容 /// </summary> /// <param name="dtA"></param> /// <param name="dtB"></param> /// <returns></returns> private string CompareDataTable(DataTable dtA, DataTable dtB) { StringBuilder strbulid = new StringBuilder(); //比內(nèi)容 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])) { //此處 不需要轉(zhuǎn)義,不然會(huì)報(bào)錯(cuò) strbulid.AppendFormat(@" " + "原值:{0} 新值:{1}", dtA.Rows[i][j].ToString(), dtB.Rows[i][j].ToString()); // return false; } } } return strbulid.ToString(); } }
?
本文摘自 :https://www.cnblogs.com/