Using SqlBulkCopy class to copy data from one table to another

SqlBulkCopy class in Asp.net 2.0 provides functionality to copy data from one table to another table. This transfer of data is very easy to perform. Check out the following code:
 

string connectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(connectionString); 
        SqlCommand myCommand = new SqlCommand("SELECT * FROM Person",myConnection);
        myConnection.Open();
        SqlDataReader dr = myCommand.ExecuteReader();
        
        SqlConnection myNewConnection = new SqlConnection(connectionString);
        myNewConnection.Open(); 
        SqlBulkCopy bulk = new SqlBulkCopy(myNewConnection);
        bulk.DestinationTableName = "[Person2]";
                          
   
        bulk.WriteToServer(dr);
        myNewConnection.Close();
        dr.Close();
        myConnection.Close();
        bulk.Close(); 

Check out the complete article Using SqlBulkCopy class in Asp.net 2.0

Print | posted @ Monday, July 18, 2005 8:22 PM

Twitter