我们怎么可以在一个数据表中的列中的数据复制到另一个,即使有不同的数据表中不同的列名? [英] How can we copy the column data of one datatable to another,even if there is different column names of different datatable?

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

问题描述

我有两个数据表。 首先是

I have two datatables. First is

DataTable NameAdressPhones = new DataTable(); 

有三列的姓名,联系地址和PHONENO 。但我只想要两列的名称和地址数据,所以我的那些列(数据)复制到新的数据表

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

DataTable NameAdress = new DataTable(); 

有关,我做的

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

现在我清楚每次NameAdressPhones(第一)的数据表,当有新的记录,在table.And每一次都会有列,但列名不一样会有所不同一样,而不是牛米 命名,添加而不是地址。现在的问题是第二个数据表中已经列名的名称和地址现在我想复制的列数据牛米,并添加第二个datatabel但列名不同于第二datatable.So即使有我要复制不同的列名的牛米列第一数据表中的数据到列第二数据表和列的命名 添加以第二数据表中的列地址第一个数据表中的数据。

Now I clear every time the NameAdressPhones(first) datatable when there are new records in the table.And every time there will be same no of columns but column name will be different like Nm instead of Name,Add instead of Address.Now problem is second datatable have already column names Name and Address and now I want to copy the columns data of Nm and Add to the second datatabel but the column names are different to the second datatable.So even If there is 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.

在短期如何才能从一个数据表中复制列数据到另一个,即使有两个数据表例如牛米是第一个数据表和名称的列名是第二个数据表中的列名则列的数据的牛米应该被复制到列命名

In short how can we copy column data from one datatable to another even if there is different column names of both datatable 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.

如果该列在同一顺序两个表中,你的可以的只是按序号而不是列名参考值,但是这是一个坏主意,我也不打算张贴任何code吧。

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天全站免登陆