Click here to Skip to main content
15,867,686 members
Articles / Database Development / SQL Server

SQL Bulk Copy with C#.NET

Rate me:
Please Sign up or sign in to vote.
4.72/5 (45 votes)
24 Jan 20072 min read 445.1K   114   43
An article about copying data to SQL within your application

Introduction

Programmers usually need to transfer production data for testing or analyzing. The simplest way to copy lots of data from any resources to SQL Server is BulkCopying. .NET Framework 2.0 contains a class in ADO.NET "System.Data.SqlClient" namespace: SqlBulkCopy. The bulk copy operation usually has two separated phases.

In the first phase, you get the source data. The source could be various data platforms such as Access, Excel, SQL.. You must get the source data in your code wrapping it in a DataTable, or any DataReader class which implements IDataReader. After that, in the second phase, you must connect the target SQL database and perform the bulk copy operation.

The bulk copy operation in .NET is a very fast way to copy large amount of data somewhere to SQL Server. The reason for that is the Bulkcopy SQL Server mechanism. Inserting all data row by row, one after the other is a very time and system resources consuming. But the bulkcopy mechanism process all data at once. So the data inserting becomes very fast.

Solution Walkthrough

While you are programming for bulk copy, first open a connection for the source data. In this sample, we are connecting a SQL Server named SQLProduction. We are using SqlConnectionStringBuilder to build our connection string.

C#
// Establishing connection
SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder(); 
cb.DataSource = "SQLProduction"; 
cb.InitialCatalog = "Sales"; 
cb.IntegratedSecurity = true;
SqlConnection cnn = new SqlConnection(cb.ConnectionString);  

Then we are retrieving data from the source with SqlCommand and SqlDataReader classes. 

C#
// Getting source data
SqlCommand cmd = new SqlCommand("SELECT * FROM PendingOrders",cnn); 
cnn.Open(); 
SqlDataReader rdr = cmd.ExecuteReader(); 

Now we have a data in rdr variable. It's time to initialize a SqlBulkCopy object and copy the data. The SqlBulkCopy class needs a connection to copy data into a SQL server. You can establish a second connection explicitly or the class will do it for you. We are using the second alternative with creating a SqlBulkCopy object. We are passing a connection string as a parameter in constructor method.

C#
// Initializing an SqlBulkCopy object
SqlBulkCopy sbc = new SqlBulkCopy("server=.;database=ProductionTest;" +
                                  "Integrated Security=SSPI"); 

OK. The sbc object is ready to copy. Now you must tell the object the destination table name, start the copying process calling WriteToServer method and pass the method the SqlDataReader variable rdr as parameter.

C#
// Copying data to destination
sbc.DestinationTableName = "Temp"; 
sbc.WriteToServer(rdr); 

At the end, close all SqlConnection, SqlDataReader and SqlBulkCopy objects.

C#
// Closing connection and the others
sbc.Close(); 
rdr.Close(); 
cnn.Close(); 

That's all. Just a few lines and in a few seconds...

C#
// Establishing connection
SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder(); 
cb.DataSource = "SQLProduction"; 
cb.InitialCatalog = "Sales"; 
cb.IntegratedSecurity = true;
SqlConnection cnn = new SqlConnection(cb.ConnectionString);  

// Getting source data
SqlCommand cmd = new SqlCommand("SELECT * FROM PendingOrders",cnn); 
cnn.Open(); 
SqlDataReader rdr = cmd.ExecuteReader(); 

// Initializing an SqlBulkCopy object
SqlBulkCopy sbc = new SqlBulkCopy("server=.;database=ProductionTest;" +
                                  "Integrated Security=SSPI"); 

// Copying data to destination
sbc.DestinationTableName = "Temp"; 
sbc.WriteToServer(rdr); 

// Closing connection and the others
sbc.Close(); 
rdr.Close(); 
cnn.Close(); 

History

  • 25th January, 2007: Initial version

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
Turkey Turkey
Kadir Çamoğlu lives in İstanbul, Turkey.
He has been programming since 1990.
He is a writer also. He wrote two books in Turkish. SQL Server 2005 Express Edition and Visual Basic 2005 Express Edition.
Recently he is working for New Horizons Turkey, Bilimer Bilişim Akademisi.

Comments and Discussions

 
GeneralRe: Backup entire database instead of one table Pin
Kadir Camoglu.6-Apr-07 20:29
Kadir Camoglu.6-Apr-07 20:29 
QuestionHow to import MS Excel sheets Pin
RavindraThakur1-Feb-07 18:17
RavindraThakur1-Feb-07 18:17 
AnswerRe: How to import MS Excel sheets Pin
LyndarEverdead2-Feb-07 10:40
LyndarEverdead2-Feb-07 10:40 
AnswerRe: How to import MS Excel sheets Pin
Kadir Camoglu.2-Feb-07 22:09
Kadir Camoglu.2-Feb-07 22:09 
GeneralRe: How to import MS Excel sheets Pin
RavindraThakur5-Feb-07 1:45
RavindraThakur5-Feb-07 1:45 
QuestionSql mobile Pin
Filip Landr2-Jan-07 21:07
Filip Landr2-Jan-07 21:07 
AnswerRe: Sql mobile Pin
Kadir Camoglu.8-Jan-07 19:25
Kadir Camoglu.8-Jan-07 19:25 
GeneralHelpful -- thanks! Pin
lukner31-Dec-06 6:12
lukner31-Dec-06 6:12 
I'm an ASP.NET newbie, and I found this article to be very helpful. I wish it might have taken an extra line or two to explain some of the flexibility in terms of selecting only particular records and collecting data from multiple tables at the source and considerations with respect to the destination table, but overall I'm very glad this article was here for my use.
AnswerRe: Helpful -- thanks! Pin
Kadir Camoglu.1-Jan-07 3:33
Kadir Camoglu.1-Jan-07 3:33 
GeneralRe: Helpful -- thanks! Pin
lukner1-Jan-07 6:05
lukner1-Jan-07 6:05 

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.