Sunday, December 25, 2011

Bulk Copy Operation


Bulk Copy Operation

This code shows you how to do bulk copy operation using C#.
string connectionString = "";
 using (SqlConnection sc = new SqlConnection(connectionString))
 {
     sc.Open();
 
     SqlCommand commandSourceData = new SqlCommand("SELECT * FROM StoresBackup;", sc);
     SqlDataReader reader =  commandSourceData.ExecuteReader();
     using (SqlConnection dc = new  SqlConnection(connectionString))
     {
         dc.Open();
         using (SqlBulkCopy bulkCopy = new SqlBulkCopy(dc))
         {
              bulkCopy.DestinationTableName = "Stores";
              bulkCopy.WriteToServer(reader);
         }
     }
 }