bulk Insert to Database(Sql server) in .net.

 ///

/// Bulk Insert
///

///Connection String
///Bulk Data in Datatable
///Table Name
/// —-Optional—-
///No of  Records will be inserted in one batch
/// true/false
public static bool BulkInsertToDatabase(DataTable dt, string tableName, int batchSize = 10000)
{
var connectionString = GetConnectionString();
using (var connection = new SqlConnection(connectionString))
{
var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.TableLock | SqlBulkCopyOptions.FireTriggers | SqlBulkCopyOptions.UseInternalTransaction, null);
bulkCopy.DestinationTableName = tableName;
connection.Open();
bulkCopy.BatchSize = batchSize;
bulkCopy.BulkCopyTimeout = 0;
bulkCopy.WriteToServer(dt);
connection.Close();
}
return true;
}

}

 

 

SHARE THIS

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.