/// <summary> /// 同步按照NewTable中ID存储情况将数据按照规则同步至OldTable,并清空OldTable中多余数据行,返回错误消息 /// </summary> public static string SetInIDPhase(DataTable OldTable, DataTable NewTable) { bool b; try { for (int i = OldTable.Rows.Count - 1; i >= 0; i--) { b = false; for (int n = 0; n < NewTable.Rows.Count; n++) { if (NewTable.Rows[n]["ID"].ToString() == OldTable.Rows[i]["ID"].ToString()) { for (int m = 0; m < NewTable.Columns.Count; m++) { OldTable.Rows[i][m] = NewTable.Rows[n][m]; } b = true; break; } } if (!b) OldTable.Rows[i].Delete(); } for (int i = 0; i < NewTable.Rows.Count; i++) { b = false; for (int n = 0; n < OldTable.Rows.Count && NewTable.Rows[i]["ID"] != DBNull.Value; n++) { if (OldTable.Rows[n].RowState == DataRowState.Deleted) continue; if (NewTable.Rows[i]["ID"].ToString() == OldTable.Rows[n]["ID"].ToString()) { b = true; break; } } if (!b) { DataRow tabRow; tabRow = OldTable.NewRow(); for (int m = 0; m < NewTable.Columns.Count; m++) { tabRow[m] = NewTable.Rows[i][m]; } OldTable.Rows.Add(tabRow); } } return string.Empty; } catch (Exception e) { return e.Message; } }
时间: 2024-10-10 09:55:34