Showing posts with label ADO NET. Show all posts
Showing posts with label ADO NET. Show all posts

Wednesday, 2 November 2016

Using SqlBulkCopy to Save Bulk Data in Sql Server

SqlBulkCopy Batch Size Property
Batch Size defines the Number of rows in each batch. At the end of each batch, the rows in the batch are sent to the server.

Let's see with an example:

We are creating 2 tables

Table 1:  

create table EmployeeBatchList
(
BatchID int,
BatchName varchar(100),
BatchDescription varchar(100)
)

Declare @Count int
set @Count=1
while @Count<5000
begin
insert into EmployeeBatchList(BatchID,BatchName,BatchDescription)
values(@Count,'BatchName - ' + cast(@Count as varchar(20)),'BatchDescription - ' +  cast(@Count as varchar(20)))
set @Count=@Count+1
end


Table 2:
create table ClientBatchList
(
ClientID int,
ClientName varchar(100),
ClientDescription varchar(100)
)

select * from EmployeeBatchList
Select * from ClientBatchList



In the aspx file put the following code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SqlBulkCopyFromOneTableToAnother.aspx.cs" Inherits="ADONET.SqlBulkCopyFromOneTableToAnother" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnLoad" runat="server" OnClick="SaveData_OnClick" Text="Save Data" /><br /><br />
        <asp:Label ID="lblMessage" runat="server" ForeColor="Green"></asp:Label>
    </div>
    </form>
</body>
</html>

  In code behind add the following code

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ADONET
{
    public partial class SqlBulkCopyFromOneTableToAnother : System.Web.UI.Page
    {
        string strMessage = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void SaveData_OnClick(object sender, EventArgs e)
        {
            String CS = ConfigurationManager.ConnectionStrings["EmplyoeeDB"].ConnectionString;
            using (SqlConnection sqlcon = new SqlConnection(CS))
            {
                SqlCommand sqlCommand = new SqlCommand("select * from EmployeeBatchList", sqlcon);
                SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "EmployeeBatchList");
                sqlcon.Open();
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlcon))
                {
                    bulkCopy.BatchSize = 500;
                    bulkCopy.NotifyAfter = 200;
                    bulkCopy.SqlRowsCopied += bulkCopy_SqlRowsCopied;
                    bulkCopy.DestinationTableName = "ClientBatchList";
                    bulkCopy.ColumnMappings.Add("BatchID", "ClientID");
                    bulkCopy.ColumnMappings.Add("BatchName", "ClientName");
                    bulkCopy.ColumnMappings.Add("BatchDescription", "ClientDescription");
                    bulkCopy.WriteToServer(ds.Tables[0]);
                }

            }
        }

        void bulkCopy_SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
        {
            strMessage += e.RowsCopied + " Rows loaded<br/>";
            lblMessage.Text = strMessage;
        }
    }
}


Lets Execute our application we will get the following output



Click on Save Data


Select * from ClientBatchList


Save XML Data into Sql Server Table using SqlBulkCopy

Let's see with an example:


We are creating a table

select * from employeeMaster
select * from employeeLocationDetails
select * from employeeDesignationDetails


DBCC CHECKIDENT ('[employeeDesignationDetails]', RESEED, 0);
Use above command to set the identity start value to 1 


Create an XML File

Employee.xml

<?xml version="1.0" encoding="utf-8" ?>
<EmployeeData>
  <Employee EmpID="1">
    <EmpName>Akhshay</EmpName>
    <DesignationId>2</DesignationId>
    <LocationId>1</LocationId>
    <Salary>32000</Salary>
  </Employee>
    <Employee EmpID="2">
    <EmpName>Sanjeet</EmpName>
    <DesignationId>1</DesignationId>
    <LocationId>2</LocationId>
    <Salary>28000</Salary>
  </Employee>
    <Employee EmpID="3">
    <EmpName>Kajal</EmpName>
    <DesignationId>3</DesignationId>
    <LocationId>2</LocationId>
    <Salary>40000</Salary>
  </Employee>
  <Location LocationId="1">
    <LocationName>Delhi</LocationName>
  </Location>
  <Location LocationId="2">
    <LocationName>Banglore</LocationName>
  </Location>
  <Designation DesignationId="1">
    <DesignationName>Developer</DesignationName>
  </Designation>
  <Designation DesignationId="2">
    <DesignationName>Designer</DesignationName>
  </Designation>
  <Designation DesignationId="3">
    <DesignationName>Manager</DesignationName>
  </Designation>
</EmployeeData>



In the aspx file put the following code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="XMLtoDatabase.aspx.cs" Inherits="ADONET.XMLtoDatabase" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btnLoad" runat="server" OnClick="SaveXMLData_OnClick" Text="Save XML Data" />
    </div>
    </form>
</body>
</html>

  In code behind add the following code

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ADONET
{
    public partial class XMLtoDatabase : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void SaveXMLData_OnClick(object sender, EventArgs e)
        {
            String CS = ConfigurationManager.ConnectionStrings["EmplyoeeDB"].ConnectionString;
            using (SqlConnection sqlcon = new SqlConnection(CS))
            {
                DataSet ds = new DataSet();
                ds.ReadXml(Server.MapPath("~/EmployeeData.xml"));

                DataTable dtEmployee = ds.Tables["Employee"];
                DataTable dtLocation = ds.Tables["Location"];
                DataTable dtDesignation = ds.Tables["Designation"];
                sqlcon.Open();
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlcon))
                {
                    bulkCopy.DestinationTableName = "employeeMaster";
                    bulkCopy.ColumnMappings.Add("EmpName", "EmpName");
                    bulkCopy.ColumnMappings.Add("DesignationId", "DesignationId");
                    bulkCopy.ColumnMappings.Add("LocationId", "LocationId");
                    bulkCopy.ColumnMappings.Add("Salary", "Salary");
                    bulkCopy.WriteToServer(dtEmployee);
                }

                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlcon))
                {
                    bulkCopy.DestinationTableName = "employeeLocationDetails";
                    bulkCopy.ColumnMappings.Add("LocationId", "LocationId");
                    bulkCopy.ColumnMappings.Add("LocationName", "LocationName");
                    bulkCopy.WriteToServer(dtLocation);
                }
               
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlcon))
                {
                    bulkCopy.DestinationTableName = "employeeDesignationDetails";
                    bulkCopy.ColumnMappings.Add("DesignationId", "DesignationId");
                    bulkCopy.ColumnMappings.Add("DesignationName", "DesignationName");
                    bulkCopy.WriteToServer(dtDesignation);
                }
            }
        }
    }

}

Lets Execute our application we will get the following output


Click on Save XML Data, data will be saved in database

select * from employeeMaster
select * from employeeLocationDetails
select * from employeeDesignationDetails