How to append one DataTable to another DataTable

Jason Kanaris picture Jason Kanaris · May 13, 2009 · Viewed 122.3k times · Source

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.

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.

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.

Answer

Xtian11 picture Xtian11 · May 17, 2013

The datatype in the same columns name must be equals.

dataTable1.Merge(dataTable2);

After that the result is:

dataTable1 = dataTable1 + dataTable2