Click here to Skip to main content
15,879,474 members
Articles / Programming Languages / SQL
Article

Transferring Data Using SqlBulkCopy

Rate me:
Please Sign up or sign in to vote.
4.95/5 (26 votes)
15 Apr 20073 min read 388.9K   119   32
An article on how to transfer data using SQLBulkCopy
Screenshot - tn_SqlBulkCopyImage2.jpg

Introduction

Transferring data from one source to another is common practice in software development. This operation is preformed in many different scenarios which includes migration of the old system to the new system, backing up the data and collecting data from different publishers. ASP.NET 2.0 includes the SqlBulkCopy class that helps to copy the data from different data sources to the SQL SERVER database. In this article, I will demonstrate the different aspects of the SqlBulkCopy class.

Database Design

The database design is pretty simple as it is based on the Products table in the Northwind database. I have created three more tables in the Northwind database. Check out the database diagram below to have a better idea.

Screenshot - tn_SqlBulkCopyImage2.jpg

The Products_Archive and Products_Latest have the same schema as the Products table while the Products_TopSelling table is different. I will explain the purpose of Products_TopSelling table later in this article.

The Products_Archive table contains 770,000 rows. You don't have to worry about how the rows got there; you just need to think about how to move all those rows in the Products_Latest table.

Transferring Data from Products_Archive to Products_Latest

SqlBulkCopy contains an instance method, WriteToServer, which is used to transfer the data from the source to the destination. WriteToServer method can perform the action of DataRow[] array, DataTable and DataReader. Depending on the situation, you can choose the container you like but in most cases, choosing DataReader is a good idea. This is because DataReader is a forward-only, read-only stream. It does not hold the data and thus is much faster then DataTable and DataRows[]. The code below is used to transfer the data from the source table to the destination table.

C#
private static void PerformBulkCopy()
{
    string connectionString =
            @"Server=localhost;Database=Northwind;Trusted_Connection=true";
    // get the source data
    using (SqlConnection sourceConnection = 
            new SqlConnection(connectionString))
    {
        SqlCommand myCommand =
            new SqlCommand("SELECT * FROM Products_Archive", sourceConnection);
        sourceConnection.Open();
        SqlDataReader reader = myCommand.ExecuteReader();

        // open the destination data
        using (SqlConnection destinationConnection =
                    new SqlConnection(connectionString))
        {
            // open the connection
            destinationConnection.Open();

            using (SqlBulkCopy bulkCopy =
            new SqlBulkCopy(destinationConnection.ConnectionString))
            {
                bulkCopy.BatchSize = 500;
                bulkCopy.NotifyAfter = 1000;
                bulkCopy.SqlRowsCopied +=
                    new SqlRowsCopiedEventHandler(bulkCopy_SqlRowsCopied);
                bulkCopy.DestinationTableName = "Products_Latest";
                bulkCopy.WriteToServer(reader);
            }
        }
        reader.Close();
    }
}

There are a couple of points to mention here. First, I am using the DataReader to fetch the rows from the database table. SqlBulkCopy class object "bulkCopy" sets the DestinationTableName property to the destination table, which in this case is "Products_Latest". Products_Latest is the destination table since the data is transferred from the Products_Archive table to the Products_Latest table. The bulkCopy object also exposes the SqlRowsCopied event which is fired after the rows identified by the NotifyAfter property has been reached. This means the event will be fired after every 1000 rows since NotifyAfter is set to 1000.

The BatchSize property is very important as most of the performance depends on it. The BatchSize means that how many rows will be send to the database at one time to initiate the data transfer. I have set the BatchSize to 500 which means that once, the reader has read 500 rows they will be sent to the database to perform the bulk copy operation. By default the BatchSize is "1" which means that each row is sent to the database as a single batch.

Different BatchSize will give you different results. You should test which batch size suits your needs.

Transferring Data Between Tables of Different Mappings

In the above example, both the tables had the same schema. Sometimes, you need to transfer the data between tables whose schema is different. Suppose you want to transfer all the product name and quantity from the Products_Archive table to the Products_TopSelling table. The schema in the two tables is different as they have different column names. This is also visible in the image above under the database design section.

C#
private static void PerformBulkCopyDifferentSchema()
{
    string connectionString = @"Server=
        localhost;Database=Northwind;Trusted_Connection=true";
    DataTable sourceData = new DataTable();
    // get the source data
    using (SqlConnection sourceConnection =
                    new SqlConnection(connectionString))
    {
        SqlCommand myCommand =
            new SqlCommand("SELECT TOP 5 * 
            FROM Products_Archive", sourceConnection);
        sourceConnection.Open();
        SqlDataReader reader = myCommand.ExecuteReader();
        // open the destination data
        using (SqlConnection destinationConnection =
                    new SqlConnection(connectionString))
        {
            // open the connection
            destinationConnection.Open();
            using (SqlBulkCopy bulkCopy =
                new SqlBulkCopy(destinationConnection.ConnectionString))
            {
                bulkCopy.ColumnMappings.Add("ProductID", "ProductID");
                bulkCopy.ColumnMappings.Add("ProductName", "Name");
                bulkCopy.ColumnMappings.Add("QuantityPerUnit", "Quantity");
                bulkCopy.DestinationTableName = "Products_TopSelling";
                bulkCopy.WriteToServer(reader);
            }
        }
        reader.Close();
    }
}

The ColumnMappings collection is used to map the column between the source table and the destination table.

Transferring Data from XML File to Database Table

The data source is not only limited to database tables, but you can also use XML files. Here is a very simple XML file which is used as a source for the bulk copy operation. (Products.xml)

XML
<?xml version="1.0" encoding="utf-8" ?>

<Products>
  <Product productID="1" productName="Chai" />
  <Product productID="2" productName="Football" />
  <Product productID="3" productName="Soap" />
  <Product productID="4" productName="Green Tea" />
</Products>

C#
private static void PerformBulkCopyXMLDataSource()
{
    string connectionString =
            @"Server=localhost;Database=Northwind;Trusted_Connection=true";
    DataSet ds = new DataSet();
    DataTable sourceData = new DataTable();
    ds.ReadXml(@"C:\Products.xml");
    sourceData = ds.Tables[0];
    // open the destination data
    using (SqlConnection destinationConnection =
                    new SqlConnection(connectionString))
    {
        // open the connection
        destinationConnection.Open();
        using (SqlBulkCopy bulkCopy =
                    new SqlBulkCopy(destinationConnection.ConnectionString))
        {
            // column mappings
            bulkCopy.ColumnMappings.Add("productID", "ProductID");
            bulkCopy.ColumnMappings.Add("productName", "Name");
            bulkCopy.DestinationTableName = "Products_TopSelling";
            bulkCopy.WriteToServer(sourceData);
        }
    }
}

The file is first read into the DataTable and then fed to the WriteToServer method of the SqlBulkCopy class. Since, the destination table is Products_TopSelling, we had to perform the column mapping.

Conclusion

In this article, I demonstrated how to use the SqlBulkCopy class which is introduced in .NET 2.0. SqlBulkCopy class makes it easier to transfer the data from a source to the SQL SERVER database.

I hope you liked the article, happy coding!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
My name is Mohammad Azam and I have been developing iOS applications since 2010. I have worked as a lead mobile developer for VALIC, AIG, Schlumberger, Baker Hughes, Blinds.com and The Home Depot. I have also published tons of my own apps to the App Store and even got featured by Apple for my app, Vegetable Tree. I highly recommend that you check out my portfolio. At present I am working as a lead instructor at DigitalCrafts.




I also have a lot of Udemy courses which you can check out at the following link:
Mohammad Azam Udemy Courses

Comments and Discussions

 
QuestionSqlBulkCopy Class Column Mapping. Pin
Member 1031091314-Feb-18 23:43
Member 1031091314-Feb-18 23:43 
Questionamazing Pin
NANDKUMAR GAIKWAD21-Jan-16 21:42
NANDKUMAR GAIKWAD21-Jan-16 21:42 
QuestionBatchSize for bulk inserts Pin
I.explore.code4-Aug-15 0:21
I.explore.code4-Aug-15 0:21 
Questionsqlbulk copy column mapping. Pin
Member 1168303527-Jun-15 20:28
Member 1168303527-Jun-15 20:28 
Questionbad examples Pin
Michael Rumpler2-May-14 21:43
Michael Rumpler2-May-14 21:43 
GeneralMy vote of 5 Pin
Shivan117-Sep-13 2:31
Shivan117-Sep-13 2:31 
QuestionThanks for the brief explanation Pin
Vikramjeet Singh18-Jun-13 21:36
Vikramjeet Singh18-Jun-13 21:36 
GeneralMy vote of 5 Pin
derekchen18-Oct-11 17:16
derekchen18-Oct-11 17:16 
GeneralMy vote of 5 Pin
Mr. Sharps8-Jun-11 10:47
Mr. Sharps8-Jun-11 10:47 
Questionsqlbulkcopy datatype mismatch Pin
Member 35026716-Jan-11 4:11
Member 35026716-Jan-11 4:11 
GeneralMy vote of 5 Pin
cristoviveyreina100025-Dec-10 20:39
cristoviveyreina100025-Dec-10 20:39 
GeneralMy vote of 5 Pin
Dr TJ6-Nov-10 19:58
Dr TJ6-Nov-10 19:58 
GeneralImport complex XML Pin
surrounding18-Aug-10 21:16
surrounding18-Aug-10 21:16 
GeneralSqlBulkCopy and DataTypes - Is it possible to mention datatype for each column when we bulk import data from excel. [modified] Pin
Karthi.Mtech9-May-10 1:23
Karthi.Mtech9-May-10 1:23 
GeneralAdd fixed field Pin
Ciupaz2-Dec-09 22:36
Ciupaz2-Dec-09 22:36 
GeneralSQLBulkCopy Pin
M.Ambigai28-May-09 12:33
M.Ambigai28-May-09 12:33 
GeneralRe: SQLBulkCopy Pin
VIJAY1316-Aug-11 23:50
VIJAY1316-Aug-11 23:50 
Generalthanks Pin
MacManzor6-May-09 19:55
MacManzor6-May-09 19:55 
GeneralRow-level error report Pin
AesopTurtle10-Oct-08 5:41
AesopTurtle10-Oct-08 5:41 
GeneralRe: Row-level error report Pin
David Catriel20-Jul-10 5:23
David Catriel20-Jul-10 5:23 
GeneralRe: Row-level error report Pin
AesopTurtle21-Jul-10 8:19
AesopTurtle21-Jul-10 8:19 
GeneralRe: Row-level error report Pin
David Catriel21-Jul-10 8:26
David Catriel21-Jul-10 8:26 
GeneralCannot access destination table Pin
PragneshMPatel7-May-08 21:45
PragneshMPatel7-May-08 21:45 
GeneralRe: Cannot access destination table Pin
la_morte15-Jun-12 1:21
la_morte15-Jun-12 1:21 
QuestionCan SqlBulkCopy copy data to the local temporary table which is dynamicly generated in a store procedure? Pin
dddd2181-Oct-07 5:04
dddd2181-Oct-07 5:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.