Using SqlBulkCopy in c#

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 =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

How to create DataGrid or GridView in JSP - Servlet

Control Webpart Visible/Enable using macro in Kentico