如何在C#中合并两个数据表 [英] How to merge two datatable in C#

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

问题描述

我有两个数据表。我想将DT与匹配和无与伦比的数据结合起来。



我尝试过的事情:



我有两个数据表。我想将DT与匹配和不匹配的数据结合起来。

I have two Datatable. I want to combine both DT with match and unmatched data.

What I have tried:

I have two Datatable. I want to combine both DT with match and unmatched data.

推荐答案

只需合并你的DataTable:

DataTable.Merge Method(DataTable)(System.Data) [ ^ ]
Just merge your DataTable:
DataTable.Merge Method (DataTable) (System.Data)[^]


仅为了您的兴趣,如果您想合并并且还需要设置一些ID或修改任何数据而不是使用此代码





foreach(DataRource NewRow in DataSource2.Rows)

{



NewRow [Source] =Second Table;

DataSource1.AddRow(NewRow);

}
Just for your interest, If you want to merge and also need to set some ID or modify any data than use this code


foreach( DataRow NewRow in DataSource2.Rows)
{

NewRow["Source"]="Second Table";
DataSource1.AddRow(NewRow );
}


dt1.merge(dt2)你有deafult的合并方法所以你可以合并两个数据表也是。



注意:如果两个表都有相同的列名,那么它们会自动合并,否则列不匹配,那么就是那个ular new column as as empty fields。
dt1.merge(dt2) u have merge method by deafult so u can merge two datatables as well.

Note: if the both tables have same column names then they merge automatically else columns mismatch r there then at that particular new column it takes as empty fields.


这篇关于如何在C#中合并两个数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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