Datatable.clone method: Clones the structure of the DataTable, including all DataTable schemas and constraints.
Datatable.copy method: Copies the structure and data of the DataTable.
We can write the following program to verify:
Copy Code code 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 table structure
Copy ()//duplicate 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);
}
}
}