SqlBulkcopy in C#
In a programming world, there are requirements where the developer need to Insert large number of records into a Database. Usually the developer uses SqlCommand class and Insert into query to insert records into database table. The SqlCommand Class inserts one record at a time into database table. So in case of large no of records, this approach will take so much time and resources. in such cases, SqlBulkCopy comes into picture.
What is SqlBulkCopy and How it is used?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace sqlbulkcopy
{
class Program
{
static DataTable dt = new DataTable();
static void Main(string[] args)
{
dt.Columns.Add("ID");
dt.Columns.Add("CustomerName");
dt.Columns.Add("Customer_Type");
dt.Columns.Add("OrderID");
dt.Columns.Add("Date");
InsertlargeData();
}
internal static void InsertlargeData()
{
string connstring = ConfigurationManager.AppSettings["DBCS"].ToString();
SqlConnection con = new SqlConnection(connstring);
con.Open();
for(int i=1;i<=10000;i++)
{
dt.Rows.Add(i,"Sandy" + i, "prime" + i,i,DateTime.Now.ToString());
}
using (var sqlbul = new SqlBulkCopy(connstring))
{
sqlbul.BatchSize = 1000;
sqlbul.NotifyAfter = 1000;
sqlbul.ColumnMappings.Add("ID", "ID");
sqlbul.ColumnMappings.Add("CustomerName", "CustomerName");
sqlbul.ColumnMappings.Add("Customer_Type", "Customer_Type");
sqlbul.ColumnMappings.Add("OrderID", "OrderID");
sqlbul.ColumnMappings.Add("Date", "Date");
sqlbul.SqlRowsCopied += new SqlRowsCopiedEventHandler(rowcopied);
sqlbul.DestinationTableName = "Customers_2";
sqlbul.WriteToServer(dt);
}
}
private static void rowcopied(object sender,SqlRowsCopiedEventArgs data)
{
Console.WriteLine("Wrote " + data.RowsCopied + " records.");
}
}
}
Outputusing System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace sqlbulkcopy
{
class Program
{
static DataTable dt = new DataTable();
static void Main(string[] args)
{
dt.Columns.Add("ID");
dt.Columns.Add("CustomerName");
dt.Columns.Add("Customer_Type");
dt.Columns.Add("OrderID");
dt.Columns.Add("Date");
InsertlargeData();
}
internal static void InsertlargeData()
{
string connstring = ConfigurationManager.AppSettings["DBCS"].ToString();
SqlConnection con = new SqlConnection(connstring);
con.Open();
for(int i=1;i<=10000;i++)
{
dt.Rows.Add(i,"Sandy" + i, "prime" + i,i,DateTime.Now.ToString());
}
using (var sqlbul = new SqlBulkCopy(connstring))
{
sqlbul.BatchSize = 1000;
sqlbul.NotifyAfter = 1000;
sqlbul.ColumnMappings.Add("ID", "ID");
sqlbul.ColumnMappings.Add("CustomerName", "CustomerName");
sqlbul.ColumnMappings.Add("Customer_Type", "Customer_Type");
sqlbul.ColumnMappings.Add("OrderID", "OrderID");
sqlbul.ColumnMappings.Add("Date", "Date");
sqlbul.SqlRowsCopied += new SqlRowsCopiedEventHandler(rowcopied);
sqlbul.DestinationTableName = "Customers_2";
sqlbul.WriteToServer(dt);
}
}
private static void rowcopied(object sender,SqlRowsCopiedEventArgs data)
{
Console.WriteLine("Wrote " + data.RowsCopied + " records.");
}
}
}
All the 10000 rows will be inserted into given Table.
Explanation
Properties
Methods
There are many methods available. But we will study few important methods.
SqlBulkcopy in C#
Reviewed by LanguageExpert
on
October 31, 2018
Rating:
No comments