Click here to Skip to main content
15,867,308 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

 
QuestionSimple but not a real world solution Pin
Inferno901-May-18 20:27
professionalInferno901-May-18 20:27 
GeneralMy vote of 4 Pin
nandakishoreroyal12-Sep-13 23:28
nandakishoreroyal12-Sep-13 23:28 
QuestionHaving problem Pin
rakibbcse1-Sep-13 1:30
rakibbcse1-Sep-13 1:30 
QuestionInsert Data Two time Pin
keyur soni22-May-13 2:00
keyur soni22-May-13 2:00 
GeneralMy vote of 1 Pin
sajad_zero3-Dec-12 21:27
sajad_zero3-Dec-12 21:27 
GeneralMy vote of 5 Pin
P.Salini21-Jun-12 21:15
P.Salini21-Jun-12 21:15 
QuestionNice tutorial Pin
therealmrcool31-May-12 2:49
therealmrcool31-May-12 2:49 
GeneralMy vote of 2 Pin
yanezricardo25-May-12 9:14
yanezricardo25-May-12 9:14 
GeneralHi, help me some thing ! Pin
Ngo Tuong Dan3-Jun-11 0:23
professionalNgo Tuong Dan3-Jun-11 0:23 
GeneralNice Article Pin
AJMAL SHAHZAD4-Mar-11 18:12
AJMAL SHAHZAD4-Mar-11 18:12 
GeneralMy vote of 5 Pin
Mohamed Arabi13-Nov-10 2:27
Mohamed Arabi13-Nov-10 2:27 
GeneralMy vote of 5 Pin
sudhakarareddy21-Sep-10 16:53
sudhakarareddy21-Sep-10 16:53 
GeneralDoes this work for Access Pin
Steve Hackathorn18-May-10 10:02
Steve Hackathorn18-May-10 10:02 
GeneralThanx Pin
infinitess24-Jan-10 16:57
infinitess24-Jan-10 16:57 
QuestionHow I know ??? Pin
arthur_hega29-Sep-09 6:06
arthur_hega29-Sep-09 6:06 
AnswerRe: How I know ??? Pin
RaviRanjanKr18-Mar-11 3:25
professionalRaviRanjanKr18-Mar-11 3:25 
GeneralThanks Pin
KilManish@gmail.com6-Aug-09 9:41
KilManish@gmail.com6-Aug-09 9:41 
General.Net Device application Pin
Masrooq-ul-islam24-May-09 22:09
Masrooq-ul-islam24-May-09 22:09 
GeneralCannot access destination table Pin
PragneshMPatel7-May-08 21:40
PragneshMPatel7-May-08 21:40 
GeneralRe: Cannot access destination table Pin
PragneshMPatel12-May-08 0:59
PragneshMPatel12-May-08 0:59 
GeneralThanks... Pin
mzielonka10-Apr-08 23:37
mzielonka10-Apr-08 23:37 
Generalbulk Copy in a MS Access database file Pin
mbv80020-Nov-07 8:33
mbv80020-Nov-07 8:33 
QuestionCan SqlBulkCopy copy data to the local temporary table which is dynamicly generated in a store procedure? Pin
dddd2181-Oct-07 5:05
dddd2181-Oct-07 5:05 
Answerthat is to say, how can I use SqlBulkCopy to copy data to the local temporary table which is dynamicly generated in a store procedure? Pin
dddd2181-Oct-07 5:10
dddd2181-Oct-07 5:10 
QuestionHow about direct Bulk Copy from CSV to SQL using "Format file"? Pin
RenaudDev10-Apr-07 5:36
RenaudDev10-Apr-07 5:36 

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.