C# - DataTable.Merge() 是否添加重复项或更新?

标签 c# .net sql-server datatable merge

我有 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/

相关文章:

c# - 计算 Int32 中的前导零

c# - 使用 JSON.NET 解析 JSON 数据

sql-server - Microsoft SQL Server 2016 - SSMS,无法扩展数据库的表

sql - 如何在 SQL Server 中模拟长时间运行的查询

c# - 驻留在 App_Code 中的类不可访问

c# - WPF DotNET 应用程序将在 Debug模式下运行,但 .EXE 将不会运行

c# - 流利的 NHibernate 错误 : The entity 'ClassMap` 1' doesn' t have an Id mapped

c# - 每次制作 dll 时如何告诉 vb6 不要创建新版本的 interfaces/com 对象?

c# - 无法使用 linq 更新

mysql - 存储过程返回错误值?