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(); } }