C#-DataTable.Merge()是否添加重复项或更新? [英] C# - Does DataTable.Merge() add duplicates or update?

查看:196
本文介绍了C#-DataTable.Merge()是否添加重复项或更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数据表. 1与SqlServer中我表中的当前行,1与我要添加的新行.

I have 2 DataTables. 1 with the current rows in my table from SqlServer and 1 with new rows that I want to add.

我想做类似的事情:

datatable1.Merge(datatable2);

我试图弄清楚这两个表之间是否存在重复项,该重复项会添加到datatable1中还是会更新datatable1中的值?

I am trying to figure out, if duplicates between these two tables exist, will the duplicate be added to datatable1, or will it update the values in datatable1?

我需要简单地在datatable1中更新重复项,而将非重复项添加到datatable1中.

I need for duplicates to simply be updated in datatable1, and non-duplicates to be added to datatable1.

我搜索了MSDN,但我找不到真正的答案.

Iv searched MSDN and I cant really find a distinct answer.

推荐答案

将新的源DataTable合并到目标中时,任何DataRowState值为Unchanged,Modified或Deleted的源行将与具有相同主键值的目标行匹配. DataRowState值为Added的源行与具有与新源行相同的主键值的新目标行匹配.

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 属性

It will update any row that has the same primary key. The primary key is set by the DataTable.PrimaryKey property

这篇关于C#-DataTable.Merge()是否添加重复项或更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆