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 :
By Mohd Zulkamal
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 =)
- Single update / insert / delete operation into database;
- Multiple update / insert / delete operation into database;
- Bulk Copy operation with transaction.
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 Mohd Zulkamal
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 =)