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 );
}
}
}