简体   繁体   English

.NET-合并两个具有相同主键的DataTable

[英].NET - Merge two DataTable with the same set of primary keys

I have two DataTable with same schema and very similar data. 我有两个具有相同架构和非常相似数据的DataTable。 One has data from web service, second from local XML file (data saved from last session). 一个来自Web服务,第二个来自本地XML文件(从上一个会话保存的数据)。

What I want to do is: 我想做的是:

  1. Merge() new data from service and old data from file Merge()来自服务的新数据和来自文件的旧数据
  2. GetChanges() to see which data has change since last session. GetChanges()以查看自上次会话以来哪些数据已更改。
  3. Perform action for all new, modified and deleted rows. 对所有新的,已修改的和已删除的行执行操作。
  4. Save data to file. 将数据保存到文件。

I have problem in step one. 我在第一步中遇到问题。 When I merge two DataTable, I got exception that primary key already exists in DataTable. 当我合并两个DataTable时,我得到一个例外,即DataTable中已经存在主键。 This is not behavior I need. 这不是我需要的行为。 In this case DataRow should be updated, not added. 在这种情况下,应该更新而不是添加DataRow。

Mayby someone know easy way to solve this problem. 也许有人知道解决此问题的简便方法。 Thanks a lot. 非常感谢。

Article linked by Honza Kovář have an example. 由HonzaKovář链接的文章有一个例子。 This code works fine and looks the same as my code. 该代码可以正常工作,并且看起来与我的代码相同。 I found problem in typed dataset I created. 我在创建的类型化数据集中发现了问题。 I have made a mistake and set field to key, not to primary key. 我犯了一个错误,并将字段设置为键,而不是主键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM