copy datatables between datasets..

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I copy a table from one dataset to another ?

code I am using and that is not working :
dataset2.tables.add(dataset1.tables(tablename))

Please help me.
prashant
 
You have to use a separate instance of the table object. There is
probably a better way, but I copy tables as follows.

Dim dtNew As New DataTable
Dim rw As DataRow

dtNew = dtOld.Clone
For Each rw In dtOld.Rows
dtNew.Rows.Add(rw.ItemArray)
Next
 
dataset2.tables.add(dataset1.tables(tablename))

Use the Dataset.Merge method
http://msdn.microsoft.com/library/d...tml/frlrfsystemdatadatasetclassmergetopic.asp

Ex:

dataset2.Merge(dataset1) 'merges entire dataset
dataset2.Merge(dataset1.Tables("tablename")) 'merges a specific table

Note:

If dataset2 is empty it will copy all tables from dataset1 into dataset2.
If dataset2 and dataset1 have tables with identical table names, this will
merge the data in dataset1 tables into dataset2 tables.
If dataset2 and dataset1 have tables with different table names, this will
copy the tables from dataset1 into dataset2.

Paul
 
Hi all,
Actually it's a very good question.
If you don't affraid of performance issues, the best solution is to use the
merge operation.
ds1.Merge(ds2) or ds1.Merge(datatable1)
But if a given table doesn't exist in the destination's ds this code
actually will create a new instance of the datatable1 using DataTable.Copy()
operation and when will add it into your destination's ds.
One can think of another solution. Suppose I could detach the desired
DataTable from the source DS and attach it to the destination's DS. I will
save using this approach the DataTable.Copy() operation which is very
expensive and will merge my data properly,
Does anybody know how to accomplish that in .NET ?
Thanks,
Uri
 
Back
Top