两个数据表彼此相邻.合并数据表 [英] two datatables to be next to eachother. merge datatable

查看:81
本文介绍了两个数据表彼此相邻.合并数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


两个数据表(dt1,dt2)已填充数据.
每个数据表都有几个字段.
字段ID在两个数据表中都是通用的.

我想有一个数据表,其中包含两个数据表的所有字段和数据.所以我尝试合并如下,但错误是:

Hi,
Two datatables (dt1, dt2) are populated with data.
Each datatable has several fields.
Field ID is common in both datatables.

I would like to have one datatable which has all the fields and data of both datatables. So I try to merge as follows but the error is:

DataTable dtAll = dt1.Copy();
dtAll.Merge(dt2);


错误是:


error is:

"Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."


你能解释我在做什么错吗?
谢谢


Can you explain what I am doing wrong please?
Thanks

推荐答案

如果您已将数据表绑定到SQL表或某些其他ADO提供程序,那么我不确定您是否可以这种方式合并.当它尝试将表中的数据绑定回ADO提供程序时.并且如错误消息所建议的那样,您的合并数据表未达到施加在表上的约束.

使用DataTable.Merge时,需要确保数据表的架构非常相似.

看到这个:

http://msdn.microsoft.com/en-us/library/fk68ew7b.aspx [ ^ ]

因此,您需要确保数据表架构兼容.然后,您需要确保要合并的数据不会破坏数据库施加的任何约束.
If you''ve bound a datatable to an SQL table or some other ADO provider then I''m not sure you can merge in this way. As it tries binding the data in the table back to the ADO provider. And as the error message suggests, your merged data table fails the constraints imposed on the table.

When using DataTable.Merge you need to make sure the schemas for the data tables are very similar.

See this:

http://msdn.microsoft.com/en-us/library/fk68ew7b.aspx[^]

So you need to make sure the data table schemas are compatible. You then need to make sure that the data you''re trying to merge is not going to break any of the constraints imposed by the database.


这篇关于两个数据表彼此相邻.合并数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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