Home All Groups Group Topic Archive Search About

Merge two DataSets to one

Author
10 Dec 2004 6:58 PM
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

Author
10 Dec 2004 7:24 PM
Mark Rae
"John Klinger" <jo***@brandtinfo.com> wrote in message
news:OOP2Bpu3EHA.2788@TK2MSFTNGP15.phx.gbl...
>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
Author
10 Dec 2004 7:45 PM
Sahil Malik
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

Show quote
"John Klinger" <jo***@brandtinfo.com> wrote in message
news:OOP2Bpu3EHA.2788@TK2MSFTNGP15.phx.gbl...
> 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

AddThis Social Bookmark Button