How can we copy the column data of one DataTable to another, even if there are different column names between DataTables?

痞子三分冷 提交于 2019-12-06 01:04:48

Here's the simplest way:

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.

Use column index number rather than names:

destRow[0] = sourcerow[0]; // for column 0 = "Name" or "NM"

If I've understood your question right, then the way this is usually done is by using stored procedures. You have the same stored procedures in both databases, but the implementation is specific to the table schema of each database. This allows you the abstraction you need.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!