Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append one DataTable to another DataTable

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 use 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.

like image 368
410 Avatar asked May 13 '09 14:05

410


People also ask

How do you append data tables?

On the Home tab, in the View group, click View, and then click Design View. On the Design tab, in the Query Type group, click Append. The Append dialog box appears. Next, you specify whether to append records to a table in the current database, or to a table in a different database.

How do I copy DataTable from one DataTable to another?

Copy() creates a new DataTable with the same structure and data as the original DataTable. To copy the structure to a new DataTable, but not the data, use Clone().


2 Answers

The datatype in the same columns name must be equals.

dataTable1.Merge(dataTable2); 

After that the result is:

dataTable1 = dataTable1 + dataTable2

like image 172
Xtian11 Avatar answered Oct 07 '22 10:10

Xtian11


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.

If you have two DataTables, use Merge.

like image 42
Thies Avatar answered Oct 07 '22 12:10

Thies