Monday, April 16, 2012

DataTable.Copy Vs DataTable.Clone

Both the methods DataTable.Clone and DataTable.Copyare are used to make copies of existing datatables, the difference is

DataTable.Clone() - Copies only the structure of the source table with the constraints, the data is not copied from the source table.

DataTable.Copy() - Copies the structure and data from the source table.

DataTable targetTable;
targetTable = sourceTable.Clone();

Here, the targetTable is created with only the structure of the sourceTable
targetTable.Rows.Count() returns 0.

Example: DataTable.Copy()
DataTable targetTable;
targetTable = sourceTable.Copy();

Here, the targetTable is created with a copy of the structure and data from the sourceTable
targetTable.Rows.Count() returns the number of rows which were present in the sourceTable.

That’s it, we have seen the difference between DataTable.Copy() and DataTable.Clone()

Search other Products:


Cindy said...

Why clone humans, reasons against cloning. Click for more information.


lingmaaki said...

clone and copy example with source code


Lee Shin said...

spot on with this write-up, i like the way you discuss the things. i'm impressed, i must say. i'll probably be back again to read more. thanks for sharing this with us.

Lee Shin

Aissa said...

Nice post. Thank you for taking the time to publish this information very informative; so happy to be given a privilege to post a comment here.