Saturday, 2 November 2013

Using SqlBulkCopy in c#

Leave a Comment
SqlBulkCopy is a powerful tools that allowed you to update/ insert/ or delete table in database with a batch update. SqlBulkCopy class offer you to load data source as long as the data can be loaded as DataTable or read with iDataReader . Using SqlBulkCopy you can perform :

  • Single update / insert /  delete operation into database;
  • Multiple update / insert /  delete operation into database;
  • Bulk Copy operation with transaction. 
This tutorial only show single update / insert / delete operation using sql bulk copy .

The C# Method Code using SqlBulkCopy

        public void InsertUsingSqlBulkCopy(DataTable tableToUpdate, string tableName, ref string err)
        {
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(Configuration.LocalDatabaseConnectionString))
            {
                bulkCopy.DestinationTableName = "dbo." + tableName;
                try
                {
                    // Write from the source to the destination.
                    bulkCopy.WriteToServer(tableToUpdate);
                }
                catch (SqlException sqlEx)
                {
                    err = sqlEx.Message;
              //static class to log the error
                    logging.writeLog(1, err, MethodBase.GetCurrentMethod().DeclaringType.Name);
                }
            }

        } 


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.