Merge two DataSets to one

  • Thread starter Thread starter John Klinger
  • Start date Start date
J

John Klinger

I need to do the following:
Create a DataSet containing one Table - no problem.
Create a second DataSet containing one Table - no problem.
Instantiate a new (third) DataSet - no problem.
"Insert" DataSet1's Table[0] in DataSet3 as its first Table - problem.
"Insert" DataSet2's Table[0] in DataSet3 as its second Table - problem.

Does anyone know how I can do this?

Thanks

___
Newsgroups brought to you courtesy of www.dotnetjohn.com
 
John Klinger said:
I need to do the following:
Create a DataSet containing one Table - no problem.
Create a second DataSet containing one Table - no problem.
Instantiate a new (third) DataSet - no problem.
"Insert" DataSet1's Table[0] in DataSet3 as its first Table - problem.
"Insert" DataSet2's Table[0] in DataSet3 as its second Table - problem.

Does anyone know how I can do this?

Instantiate the two new tables in DataSet3.

Iterate through the Rows collection in DataSet1's Table[0] & DataSet2's
Table[0] and "import" them into the Table objects in DataSet3 using the
ImportRow method
 
John,

Here's sample code ----

static void Main(string[] args)
{
DataSet ds1 = new DataSet() ;
ds1.Tables.Add(new DataTable("Table1"));

DataSet ds2 = new DataSet();
ds2.Tables.Add(new DataTable("Table2"));

DataSet ds3 = new DataSet();
ds3.Tables.Add(new DataTable("Table3"));

DataTable tempdt3 = ds3.Tables[0];
ds3.Tables.Remove("Table3");

DataTable tempdtx = ds1.Tables[0];
ds1.Tables.Remove("Table1");
ds3.Tables.Add(tempdtx);

tempdtx = ds2.Tables[0];
ds2.Tables.Remove("Table2");
ds3.Tables.Add(tempdtx);

ds3.Tables.Add(tempdt3);

Console.WriteLine(ds3.Tables[0].TableName);
Console.WriteLine(ds3.Tables[1].TableName);
Console.WriteLine(ds3.Tables[2].TableName);
}

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
Back
Top