SQLBulkCopy in C#

In development sometimes we need to transfer a large amount of data from sources to a SQL Server database table. We can do this kind of stuff using sqlbulkcopy. Bulk means many things. So with the use of SqlBulkCopy you can send a large amount of data from any source to SQL Server database. For example you have a collection of data in XML format and you want to save this data into your database table. You can do this very easily. Today in this article we will learn how to use SqlBulkCopy in C#.

The SqlBulkCopy class is part of the System.Data.SqlClient namespace. In this class you have always 2 parts, the source and the destination. The source could be XML, Access, Excel or SQL (in other words any type of datasource you have). This data can be loaded into a datatable or datareader after the destination portion of the data is inserted very quickly into the table.

How it works

Have a look at the following example:

  1. Open Visual Studio.
  2. "File" -> "New" -> "Project..."then select ASP.Net Webform Application.
  3. Add New web form.

In this example, we have a XML file data.xml. In that file we have some student records.

Data.xml

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <data>  
  3.   <Student Id="1">  
  4.     <Name>Sourabh Mishra</Name>  
  5.     <Phone>9999999999</Phone>  
  6.     <Address>Delhi</Address>  
  7.     <Class>MCA</Class>  
  8.     <IsActive>1</IsActive>  
  9.   </Student>  
  10.   <Student Id="2">  
  11.     <Name>Surbhee Mishra</Name>  
  12.     <Phone>9123456789</Phone>  
  13.     <Address>Delhi</Address>  
  14.     <Class>BTech</Class>  
  15.     <IsActive>1</IsActive>  
  16.   </Student>  
  17.   <Student Id="3">  
  18.     <Name>Sachin</Name>  
  19.     <Phone>9123499999</Phone>  
  20.     <Address>Mumbai</Address>  
  21.     <Class>Bsc</Class>  
  22.     <IsActive>1</IsActive>  
  23.   </Student>  
  24.   <Student Id="4">  
  25.     <Name>James</Name>  
  26.     <Phone>9123499007</Phone>  
  27.     <Address>London</Address>  
  28.     <Class>Ms</Class>  
  29.     <IsActive>1</IsActive>  
  30.   </Student>  
  31. </data>  
In this example we have a StudentMaster table where we stored the preceding XML data.

Webform.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SqlBulkCopyDemo.WebForm1" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <asp:Button ID="Button1" runat="server" Text="Load Bulk Data" OnClick="Button1_Click" />  
  13.     </div>  
  14.     </form>  
  15. </body>  
  16. </html>  
WebForm1.aspx.cs
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         string cs = ConfigurationManager.ConnectionStrings["SchoolConnectionString"].ConnectionString;  
  6.         using (SqlConnection sqlConn = new SqlConnection(cs))  
  7.         {  
  8.             DataSet ds = new DataSet();  
  9.             ds.ReadXml(Server.MapPath("~/Data.xml"));  
  10.             DataTable dtStudentMaster = ds.Tables["Student"];  
  11.             sqlConn.Open();  
  12.             using (SqlBulkCopy sqlbc = new SqlBulkCopy(sqlConn))  
  13.             {  
  14.                 sqlbc.DestinationTableName = "StudentMaster";  
  15.                 sqlbc.ColumnMappings.Add("Name""Name");  
  16.                 sqlbc.ColumnMappings.Add("Phone""Phone");  
  17.                 sqlbc.ColumnMappings.Add("Address""Address");  
  18.                 sqlbc.ColumnMappings.Add("Class""Class");  
  19.                 sqlbc.WriteToServer(dtStudentMaster);  
  20.                 Response.Write("Bulk data stored successfully");  
  21.             }  
  22.         }  
  23.     }  
  24.     catch (Exception ex)  
  25.     {  
  26.         throw ex;  
  27.     }  
  28. }  
Understand the code

Look at the preceding code, in this code we have dataset where we read XML data and store the data into a DataTable.
  1. DataSet ds = new DataSet();  
  2. ds.ReadXml(Server.MapPath("~/Data.xml"));  
  3. DataTable dtStudentMaster = ds.Tables["Student"];  
Then we create an object of SqlBulkCopy class, in this class we have some properties.
  1. using (SqlBulkCopy sqlbc = new SqlBulkCopy(sqlConn))  
  2. {  
  3.     sqlbc.DestinationTableName = "StudentMaster";  
  4.     sqlbc.ColumnMappings.Add("Name""Name");  
  5.     sqlbc.ColumnMappings.Add("Phone""Phone");  
  6.     sqlbc.ColumnMappings.Add("Address""Address");  
  7.     sqlbc.ColumnMappings.Add("Class""Class");  
  8.     sqlbc.WriteToServer(dtStudentMaster);  
  9.     Response.Write("Bulk data stored successfully");  
  10. }  
Look at the preceding code, we have a sqlbc object here we have a DestinationTable property, so here we need to define a table name where we store that data. In the next line we have ColumnMappings where we must define a Source column and a Destination column.

SQLBC

Then we have a WriteToServer method where we define the datatable. This datatable copies all the data into the destination table.

That's it, press F5 and run your code.

local host

query output

You can see in the StudentMaster table that all the data from the XML file is stored into our database table.

 

Up Next
    Ebook Download
    View all
    Learn
    View all