我有 2 个数据表。 1 包含来自 SqlServer 的表中的当前行,1 包含我要添加的新行。
我想做这样的事情:
datatable1.Merge(datatable2);
我想弄清楚,如果这两个表之间存在重复项,重复项是否会添加到 datatable1 中,还是会更新 datatable1 中的值?
我需要在 datatable1 中简单地更新重复项,并将非重复项添加到 datatable1 中。
我搜索了 MSDN,但找不到明确的答案。
最佳答案
来自 Merge documentaton 的最后一段
When merging a new source DataTable into the target, any source rows with a DataRowState value of Unchanged, Modified, or Deleted, is matched to target rows with the same primary key values. Source rows with a DataRowState value of Added are matched to new target rows with the same primary key values as the new source rows.
它将更新具有相同主键的任何行。主键由 DataTable.PrimaryKey
设置属性(property)
关于C# - DataTable.Merge() 是否添加重复项或更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34819752/