Editorial / Best Answer
samiksc
The question can mean 2 tasks -
1. Copy one 'datatable' to another and delete the source 'datatable' OR
2. Copy one database table (say a table in sql server database) to another database table, and delete the source table which is in the SQL server database. This operation is to be performed using ADO.Net
Answers:
1. DataTable newOne = originalOne.Copy(); originalOne.Dispose(); -- Note that originalOne.Clear() will simply delete all rows from the table, but the table object with its structure will remain there.
2. Run DDL commands using 'ExecuteNonQuery' method of DataCommand object.
e.g. string cmdText1 = "create table newOne as select * from originalOne" (copy table to another)
string cmdText2 = "drop table originalOne";
DataCommand dataCmd = new SqlDataCommand(cmdText1, conn);
conn.Open();
dataCmd.ExecuteNonQuery();
dataCmd.CommandText = cmdText2;
dataCmd.ExecuteNonQuery();
conn.Close();
How to copy the contents from one table to another table and how to delete the source table in ado.net?
Editorial / Best Answer
samikscProfile Answers by samiksc Questions by samiksc
The question can mean 2 tasks -
1. Copy one 'datatable' to another and delete the source 'datatable' OR
2. Copy one database table (say a table in sql server database) to another database table, and delete the source table which is in the SQL server database. This operation is to be performed using ADO.Net
Answers:
1. DataTable newOne = originalOne.Copy(); originalOne.Dispose(); -- Note that originalOne.Clear() will simply delete all rows from the table, but the table object with its structure will remain there.
2. Run DDL commands using 'ExecuteNonQuery' method of DataCommand object.
e.g. string cmdText1 = "create table newOne as select * from originalOne" (copy table to another)
string cmdText2 = "drop table originalOne";
DataCommand dataCmd = new SqlDataCommand(cmdText1, conn);
conn.Open();
dataCmd.ExecuteNonQuery();
dataCmd.CommandText = cmdText2;
dataCmd.ExecuteNonQuery();
conn.Close();
Related Answered Questions
Related Open Questions