合并2数据表和存储在一个新的 [英] Merge 2 DataTables and store in a new one

查看:94
本文介绍了合并2数据表和存储在一个新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有2个数据表(dtOne和dtTwo),我想合并它们,把他们在其他DataTable(dtAll)。我怎样才能做到这在C#?我尝试在数据表合并声明,但这种返回void。是否合并preserve的数据?例如,如果我做的:

  dtOne.Merge(dtTwo);

请问dtOne改变或不改变dtTwo,如果任何一个变化,做的修改preserve?

我知道我不能这样做,因为合并返回void,但我希望能够既dtOne和dtTwo合并存储dtAll:

  //将无法工作,我如何做到这一点
dtAll = dtOne.Merge(dtTwo);


解决方案

合并方法从第二个表中获取价值和与第一表合并它们,所以第一,现在会从两个保存的值。

如果您想preserve无论是原表,你可以复制原来的第一,然后合并:

  dtAll = dtOne.Copy();
dtAll.Merge(dtTwo);

If I have 2 DataTables (dtOne and dtTwo) and I want to merge them and put them in another DataTable (dtAll). How can I do this in C#? I tried the Merge statement on the datatable, but this returns void. Does Merge preserve the data? For example, if I do:

 dtOne.Merge(dtTwo);

Does dtOne change or does dtTwo change and if either one changes, do the changes preserve?

I know I can't do this because Merge returns void, but I want to be able to store the Merger of both dtOne and dtTwo in dtAll:

//Will Not work, How do I do this
dtAll = dtOne.Merge(dtTwo);

解决方案

The Merge method takes the values from the second table and merges them in with the first table, so the first will now hold the values from both.

If you want to preserve both of the original tables, you could copy the original first, then merge:

dtAll = dtOne.Copy();
dtAll.Merge(dtTwo);

这篇关于合并2数据表和存储在一个新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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