即使数据表之间的列名称不同,如何将一个数据表的列数据复制到另一数据表中? [英] How can we copy the column data of one DataTable to another, even if there are different column names between DataTables?

查看:142
本文介绍了即使数据表之间的列名称不同,如何将一个数据表的列数据复制到另一数据表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据表。
首先是

I have two DataTables. First is

DataTable NameAddressPhones = new DataTable(); 

有三列姓名,地址和电话号码。但是我只想两列列名称和地址数据,因此我想将这些列(包含数据)复制到新的DataTable中。

with Three columns Name, Address and PhoneNo.But I only want two columns Name and Address data so I want to copy those columns (with data) to the new DataTable.

DataTable NameAddress = new DataTable(); 

为此我

            foreach (DataRow sourcerow in NameAddressPhones.Rows)
            {
                DataRow destRow = NameAddress.NewRow();
                foreach (string colname in columns)
                {
                    destRow[colname] = sourcerow[colname];
                }
                NameAddress.Rows.Add(destRow);
            }

每次插入新记录时,我都会清除NameAddressPhones(first)DataTable。桌子。而且,每次列数都相同,但是列名会有所不同,例如 Nm 而不是 Name,Add 而不是 Address 。现在的问题是第二个DataTable已经具有列名 Name and Address ,现在我想将 Nm和Add 的列数据复制到第二个DataTable中,但是列名是与第二个DataTable的列名不同。因此,即使列名不同,我也想将第一个DataTable的 Nm 列数据复制到第二个DataTable的 Name 列和 Add 列将第一个DataTable的数据复制到第二个DataTable的 Address 列。

I clear the NameAddressPhones(first) DataTable every time there are new records inserted in the table. And every time there will be the same number of columns but the column names will be different like Nm instead of Name, Add instead of Address.Now the problem is the second DataTable already has column names Name and Address and now I want to copy the columns data of Nm and Add to the second DataTable but the column names are different than the column names of the second DataTable. So even if there are different column names I want to copy Nm column data of first DataTable to the column Name of second DataTable and column Add data of first DataTable to column Address of second DataTable.

简而言之,即使存在不同的数据,如何将列数据从一个DataTable复制到另一个数据表两个数据表的列名(例如 Nm )是第一个数据表的列名, Name 是第二个数据表的列名,然后是 Nm 列的数据>应该复制到名称列中。

In short how can we copy column data from one DataTable to another even if there are different column names of both DataTables like Nm is the column name of first DataTable and Name is the column name of second DataTable then the data of the column Nm should be copied to the column Name.

推荐答案

这是最简单的方法:

foreach (DataRow sourcerow in NameAdressPhones.Rows)
{
    DataRow destRow = NameAdress.NewRow();
    destRow["Name"] = sourcerow["Nm"];
    destRow["Address"] = sourcerow["Add"];
    NameAdress.Rows.Add(destRow);
}

自动化程度很高。否则,您必须以某种方式将源列映射到目标列。

Automation is great when it's available. When it's not, you have to map source columns to destination columns in some manner.

如果两个表中的列顺序相同,则可以只是按序号而不是列名引用值,但这是一个坏主意,我什至不打算为其发布任何代码。

If the columns are in the same order in both tables, you could just reference the values by ordinal instead of column name, but that's such a bad idea I'm not even going to post any code for it.

这篇关于即使数据表之间的列名称不同,如何将一个数据表的列数据复制到另一数据表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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