Difference between the DataTable Clone method and the Copy method

Source: Internet
Author: User

DataTable. Clone method: Clone the DataTable structure, including all DataTable architectures and constraints.

DataTable. Copy method: Copy the structure and data of the DataTable.

We can write the following program for verification:

Copy codeThe Code is as follows: static string connStr = "Server =. \ sqlexpress; Initial Catalog = hr; Integrated Security = True ";

Static void Clone ()
{
Using (SqlConnection conn = new SqlConnection (connStr ))
{
String SQL = "select * from emp ";
SqlDataAdapter da = new SqlDataAdapter (SQL, conn );
DataTable dt = new DataTable ();
Da. Fill (dt );
DataTable dtClone = dt. Clone ();
Print (dtClone );
}
}

Private static void Print (DataTable dtClone)
{
Foreach (DataColumn col in dtClone. Columns)
{
Console. Write (col. DataType + "\ t ");
}
Console. WriteLine ();
Foreach (DataRow row in dtClone. Rows)
{
Console. Write (row [0] + "\ t" + row [1] + "\ t" + row [2] + "\ n ");
}
Console. WriteLine ();
}
Static void Main (string [] args)
{
Clone (); // only copy the table structure
Copy (); // Copy the table structure and data
Console. ReadKey ();
}

Private static void Copy ()
{
Using (SqlConnection conn = new SqlConnection (connStr ))
{
String SQL = "select * from emp ";
SqlDataAdapter da = new SqlDataAdapter (SQL, conn );
DataTable dt = new DataTable ();
Da. Fill (dt );
DataTable dtCopy = dt. Copy ();
Print (dtCopy );
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.