C# 如何将一个数据表附加到另一个数据表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/858401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to append one DataTable to another DataTable
提问by Jason Kanaris
I would like to append one DataTable to another DataTable. I see the DataTable class has two methods; "Load(IDataReader)" and "Merge(DataTable)". From the documentation, both appear to 'merge' the incoming data with the existing DataTable if rows exist. I will be doing the merge in a data access layer.
我想将一个 DataTable 附加到另一个 DataTable。我看到 DataTable 类有两个方法;“加载(IDataReader)”和“合并(数据表)”。从文档中,如果存在行,两者似乎都将传入数据与现有数据表“合并”。我将在数据访问层中进行合并。
I could could usa an IDataReader and use the Load method to merge the DataTables. Or I could load a DataSet using the IDataReader, get the DataTable from the DataSet, and then use the Merge method to merge the DataTables.
我可以使用 IDataReader 并使用 Load 方法来合并数据表。或者我可以使用 IDataReader 加载数据集,从数据集中获取数据表,然后使用 Merge 方法合并数据表。
I was wondering if someone could tell me which is the proper method to use?
我想知道是否有人可以告诉我使用哪种方法是正确的?
Alternatively, let me know if you have a different suggestion on how to accomplish this task.
或者,如果您对如何完成此任务有不同的建议,请告诉我。
采纳答案by Thies
Merge takes a DataTable, Load requires an IDataReader - so depending on what your data layer gives you access to, use the required method. My understanding is that Load will internally call Merge, but not 100% sure about that.
Merge 需要一个 DataTable,Load 需要一个 IDataReader - 因此根据您的数据层允许您访问的内容,使用所需的方法。我的理解是 Load 会在内部调用 Merge,但不能 100% 确定。
If you have two DataTables, use Merge.
如果您有两个数据表,请使用合并。
回答by Sam Axe
You could let your DataAdapter
do the work. DataAdapter.Fill(DataTable)
will append your new rows to any existing rows in DataTable
.
你可以让你DataAdapter
的工作。 DataAdapter.Fill(DataTable)
将您的新行附加到DataTable
.
回答by Xtian11
The datatype in the same columns name must be equals.
相同列名中的数据类型必须相等。
dataTable1.Merge(dataTable2);
After that the result is:
之后的结果是:
dataTable1 = dataTable1 + dataTable2
数据表1 = 数据表1 + 数据表2
回答by Ramesh Venkataswamy
Add two datasets containing datatables, now it will merge as required
添加两个包含数据表的数据集,现在它会根据需要合并
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
DataTable dt1 = new DataTable();
dt1.Columns.Add(new DataColumn("Column1", typeof(System.String)));
DataRow newSelRow1 = dt1.NewRow();
newSelRow1["Column1"] = "Select";
dt1.Rows.Add(newSelRow1);
DataTable dt2 = new DataTable();
dt2.Columns.Add(new DataColumn("Column1", typeof(System.String)));
DataRow newSelRow2 = dt1.NewRow();
newSelRow2["Column1"] = "DataRow1Data"; // Data
dt2.Rows.Add(newSelRow2);
ds1.Tables.Add(dt1);
ds2.Tables.Add(dt2);
ds1.Tables[0].Merge(ds2.Tables[0]);
Now ds1 will have the merged data
现在 ds1 将有合并的数据
回答by Waheed Alzekry
use loop
使用循环
for (int i = 0; i < dt1.Rows.Count; i++)
{
dt2.Rows.Add(dt1.Rows[i][0], dt1.Rows[i][1], ...);//you have to insert all Columns...
}
回答by Derpy
Consider a solution that will neatly handle arbitrarily many tables.
考虑一个可以巧妙处理任意多个表的解决方案。
//ASSUMPTION: All tables must have the same columns
var tables = new List<DataTable>();
tables.Add(oneTableToRuleThemAll);
tables.Add(oneTableToFindThem);
tables.Add(oneTableToBringThemAll);
tables.Add(andInTheDarknessBindThem);
//Or in the real world, you might be getting a collection of tables from some abstracted data source.
//behold, a table too great and terrible to imagine
var theOneTable = tables.SelectMany(dt => dt.AsEnumerable()).CopyToDataTable();
Encapsulated into a helper for future reuse:
封装成一个 helper 以备将来重用:
public static DataTable CombineDataTables(params DataTable[] args)
{
return args.SelectMany(dt => dt.AsEnumerable()).CopyToDataTable();
}
Just have a few tables declared in code?
只是在代码中声明了几个表?
var combined = CombineDataTables(dt1,dt2,dt3);
Want to combine into one of the existing tables instead of creating a new one?
想要合并到现有表之一而不是创建新表吗?
dt1 = CombineDataTables(dt1,dt2,dt3);
Already have a collection of tables, instead of declared one by one?
已经有一个表的集合,而不是一个一个地声明?
//Pretend variable tables already exists
var tables = new[] { dt1, dt2, dt3 };
var combined = CombineDataTables(tables);