views:

3767

answers:

3

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.

+3  A: 

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.

Thies
A: 

If you want to add rows, even if they already exist, you should use the Merge() method and be sure not to define a PrimaryKey.

Jon Freeland
A: 

You could let your DataAdapter do the work. DataAdapter.Fill(DataTable) will append your new rows to any existing rows in DataTable.

Boo