Copying the content of a DataTable

B

Bruce Schechter

I fear that I am asking a very elementary question, but never-the-less I
need help so here goes...

I need to simply copy the contents of one DataTable into another DataTable.
My application looks something like this....

DataTable dt1, dt2;
dt1 = new DataTable();
// Read dt1 from database
....
// Here is where I need help... I need to copy the contents of dt1 into
dt2.
// Then, ...do some drastic manipulation of dt2 but need to keep the
original content within dt1.
DataView dv = new DataView( dt2 );
DataGrid.DataSource = dv;
.... etc.

As for how to copy the DataTable, I found the MemberwiseClone() method, but
it turns out to be "protected" so I get a compiler error when I try to use
it. Is there a way to use it? Or, is there a better way?

Also, note that I don't want to do a crude copy solution like iterating
through all the rows and elements to re-create dt2, because I don't want to
lose all the excellent "metadata" (like column names, types, etc.) that
ADO.NET so cleverly binds into a DataTable read from a database.

I'm new to the .NET game, so forgive me if I'm missing some obvious points.

Cheers, Bruce
 
K

Kevin Yu [MSFT]

Hi Bruce,

There doesn't seem to be a method provided by DataTable to clone itself.
However, there's some workarounds to achieve this.

1. I've checked your posted code that you need to clone the DataTable after
the source table is filled by an Adapter. You can fill it the other table
just as you did on the source table, like the following:

dataadapter.Fill(dt1);
dataadapter.Fill(dt2);

2. If you need to clone it after it is modified, you can fill the data into
a DataSet, write the DataSet to an XML file, and read it from another
DataSet. The DataTable is contained in the DataSet.

dataadapter.Fill(dataset1,"Table1");
dataset1.WriteXml(@"c:\aaa.xml");
dataset2.ReadXml(@"c:\aaa.xml");
dt2 = dataset2.Table["Table1"];

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "Bruce Schechter" <[email protected]>
| Subject: Copying the content of a DataTable
| Date: Thu, 30 Oct 2003 00:11:17 -0800
| Lines: 32
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.adonet
| NNTP-Posting-Host: w127.z208177132.sjc-ca.dsl.cnc.net 208.177.132.127
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:64884
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| I fear that I am asking a very elementary question, but never-the-less I
| need help so here goes...
|
| I need to simply copy the contents of one DataTable into another
DataTable.
| My application looks something like this....
|
| DataTable dt1, dt2;
| dt1 = new DataTable();
| // Read dt1 from database
| ...
| // Here is where I need help... I need to copy the contents of dt1 into
| dt2.
| // Then, ...do some drastic manipulation of dt2 but need to keep the
| original content within dt1.
| DataView dv = new DataView( dt2 );
| DataGrid.DataSource = dv;
| ... etc.
|
| As for how to copy the DataTable, I found the MemberwiseClone() method,
but
| it turns out to be "protected" so I get a compiler error when I try to use
| it. Is there a way to use it? Or, is there a better way?
|
| Also, note that I don't want to do a crude copy solution like iterating
| through all the rows and elements to re-create dt2, because I don't want
to
| lose all the excellent "metadata" (like column names, types, etc.) that
| ADO.NET so cleverly binds into a DataTable read from a database.
|
| I'm new to the .NET game, so forgive me if I'm missing some obvious
points.
|
| Cheers, Bruce
|
|
|
 
K

Kevin Yu [MSFT]

Thanks CT for your reply,

Hi Bruce,

you can use DataTable.Copy method to copy both the structure and data. For
more information, please refer to the following link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatatableclasscopytopic.asp

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "CT" <[email protected]>
| References: <[email protected]>
| Subject: Re: Copying the content of a DataTable
| Date: Thu, 30 Oct 2003 09:49:15 +0100
| Lines: 48
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.adonet
| NNTP-Posting-Host: 0x50a430be.esnxx3.adsl-dhcp.tele.dk 80.164.48.190
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:64886
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| Bruce,
|
| Check out the Copy method of the DataTable class.
|
| --
| Carsten Thomsen
| Enterprise Development with VS .NET, UML, and MSF
| http://www.apress.com/book/bookDisplay.html?bID=105
| | > I fear that I am asking a very elementary question, but never-the-less I
| > need help so here goes...
| >
| > I need to simply copy the contents of one DataTable into another
| DataTable.
| > My application looks something like this....
| >
| > DataTable dt1, dt2;
| > dt1 = new DataTable();
| > // Read dt1 from database
| > ...
| > // Here is where I need help... I need to copy the contents of dt1
into
| > dt2.
| > // Then, ...do some drastic manipulation of dt2 but need to keep the
| > original content within dt1.
| > DataView dv = new DataView( dt2 );
| > DataGrid.DataSource = dv;
| > ... etc.
| >
| > As for how to copy the DataTable, I found the MemberwiseClone() method,
| but
| > it turns out to be "protected" so I get a compiler error when I try to
use
| > it. Is there a way to use it? Or, is there a better way?
| >
| > Also, note that I don't want to do a crude copy solution like iterating
| > through all the rows and elements to re-create dt2, because I don't want
| to
| > lose all the excellent "metadata" (like column names, types, etc.) that
| > ADO.NET so cleverly binds into a DataTable read from a database.
| >
| > I'm new to the .NET game, so forgive me if I'm missing some obvious
| points.
| >
| > Cheers, Bruce
| >
| >
|
|
|
 
C

Cor

Hi Bruce,

Yes it is elemantary but you can say strange that it has to do in this way.
For a datasets
dataset2=dataset1.copy()

I hope that it was this you were looking for?

Cor
 
B

Bruce Schechter

Hello All,
Thanks for all the replies. I got exactly what I needed, i.e, the
DataTable.Copy() method. (For some reason I had found the .Clone() method,
which didnt copy the data, and missed out on the Copy() method on my
searches of MSDN documentation. But you all remedied that.)
Thanks again,
Cheers, Bruce
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top