Welcome to BARMAGY Sign in | Join | Help

SqlBulkCopy – one major feature in the ADO.NET 2.0

There was in the past lots of approaches to transfer data from one table to another those methods deferred in its complexity or in the performance impact behind applying it,
We face this scenario everyday in our real life projects and even in our small code snippets that support us in the data migration process while project deployments and so on…

Taking the above scenarios in our minds the ADO.NET 2.0 introduced the SqlBulkCopy class which work as a schema mapping between two data sources and starts transferring data from a source data table to another.

This operation takes the following steps:-

1-      We define the data source and the targeted destination

2-      Then we start mapping the data attributes – columns – between the source and the destination tables.

3-      After that we start configuring the BatchSize – the number of rows to be copy in each attempt to copy- and the BulkCopyTimeOut.

4-      We also have a SqlRowsCopied event which get fired whenever the number we define in the NotifyAfter property got reached.

Let’s go to the enjoyable part which is CODE J

protected void MoveData(DataSet dsSource)

        {

          

            using (SqlConnection connDest = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString()))

            {

                using (SqlBulkCopy bulkCopyobj = new SqlBulkCopy(connDest))

                {

                    //Start mapping columns

                    bulkCopyobj.ColumnMappings.Add("SourceID", "DestID");

                   

                    //configure the SqlBulkCopy Properties

                    bulkCopyobj.BatchSize = 90;

                    bulkCopyobj.BulkCopyTimeout = 15;

                    bulkCopyobj.NotifyAfter = 250;

                    bulkCopyobj.DestinationTableName = "DestinationTable";

                    bulkCopyobj.SqlRowsCopied += new SqlRowsCopiedEventHandler(bulkCopyobj_SqlRowsCopied);

                    //start moving data

                    bulkCopyobj.WriteToServer(dsSource.Tables[0]);

  //close the bulk copy operation

                    bulkCopyobj.Close();

 

 

                }

            }

          

 

        }

 

        void bulkCopyobj_SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)

        {

            Console.Write(string.Format("{0} Rows copied....",e.RowsCopied.ToString()) );

        }

        protected DataSet GetData()

        {

            using (SqlConnection connSource = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionSource"].ToString()))

            {

                SqlDataAdapter da = new SqlDataAdapter("GetDataFromSource", connSource);

                DataSet ds = new DataSet();

                da.SelectCommand.CommandType = CommandType.StoredProcedure;

                da.Fill(ds);

                return ds;

            }

        }

Published Friday, February 09, 2007 1:25 PM by hus
Filed Under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: SqlBulkCopy – one major feature in the ADO.NET 2.0

Wednesday, March 23, 2011 9:57 AM by mumUnopeChume
krayolad1|Thanks to ray...@fandeyyп»ї
 
<a href="http://canadianstore.org/?p=605">The weight loss industry&#8217;s instilled fear of food</a>

yrsaawwsw2

# re: SqlBulkCopy – one major feature in the ADO.NET 2.0

Tuesday, March 29, 2011 4:08 AM by loan
I guess that to receive the loan from creditors you should present a good reason. Nevertheless, once I've got a short term loan, just because I wanted to buy a house.

# re: SqlBulkCopy – one major feature in the ADO.NET 2.0

Sunday, April 10, 2011 4:02 PM by Luck
yPVWcx That's way the bestest answer so far!

What do you think?

(required) 
required 
(required)