Answered Questions

  • How to copy the contents from one table to another table and how to delete the source table in ado.net?

    Star Read Best Answer

    Editorial / Best Answer

    samiksc  

    • Member Since Oct-2005 | Jul 12th, 2007


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

    sathin

    • Oct 29th, 2009

    This is definitely possible. I tried making it simple by talking two gridviews in the design part and writing the below code when you try creating a datatable do avoid the new keyword Imports S...