Bulk Copy Data Using SQLBulkCopy Class
SQLBulkCopy class in ADO.NET can be used to do bulk copy operations from a .Net application. There are two ways data can be bulk copied using this class. You can either do a single bulk copy or a multiple bulk copy operation. In this post I will show you how to perform a single bulk copy operation. I will copy all data in [order details] table from one Northwind database to another. Before running the code I have already cleared the [order details] table in destination database using a truncate statement.
Here is a complete sample which copies data from my Northwind database to Northwind2 database:
static void Main(string[] args)
{
/******************************************************************
* Bulk copy Order Details data from Northwind to Northwind2 database
*****************************************************************/
string northwindConnectionString =
"Data Source=.;Initial Catalog=Northwind;Integrated Security=True";
string northwind2ConnectionString =
"Data Source=.;Initial Catalog=Northwind2;Integrated Security=True";
using (SqlConnection northwindConnection =
new SqlConnection(northwindConnectionString))
{
northwindConnection.Open();
SqlCommand command =
new SqlCommand("Select * from [order details]", northwindConnection);
SqlDataReader dataReader = command.ExecuteReader();
using (SqlConnection northwind2Connection =
new SqlConnection(northwind2ConnectionString))
{
northwind2Connection.Open();
SqlBulkCopy bulkCopy = new SqlBulkCopy(northwind2Connection);
bulkCopy.DestinationTableName = "[Order Details]";
bulkCopy.WriteToServer(dataReader);
northwind2Connection.Close();
}
northwindConnection.Close();
}
Console.WriteLine("Bulk Copy Operation Successful");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
While executing the above code, I also ran profiler to see the generated statements. SQLBulkCopy produced the following SQL statement for this bulk copy operation.
insert bulk [Order Details] ([OrderID] Int,
[ProductID] Int,
[UnitPrice] Money,
[Quantity] SmallInt,
[Discount] Real)
SQLBulkCopy is a fast efficient way to do bulk copy operations. It is useful when you are handling large amount of data for simple operations such as copying from one location to another.
4 Responses to Bulk Copy Data Using SQLBulkCopy Class
Leave a Reply Cancel reply
Top Posts
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Windows 7 Control Panel In Classic Mode
- More Details Emerge On Microsoft Master Certification
- Use SqlConnection With LINQ To SQL
- Free Icons And Images With Visual Studio 2008
- Capture XML In WCF Service
- Dynamic Sort With LINQ
- StyleCop Tutorial
Tags
.Net 2010 ADO.NET ASP.NET Azure Blogging Books Browsers C# Certification Cloud Computing Code Snippets Community Data Services Eclipse Entity Framework Google IDE Java LINQ Mac Microsoft Museum NetBeans Office Oracle REST SharePoint Silverlight SQL Server T-SQL Tips Tools Training Visual Studio Visual Studio 2010 WCF Web Windows Windows 7 Windows Forms Windows Live WMI WPF XAML


Hi,
I have a XLS File and need upload to sql server, I use a sqlbulkcopy…when one throw me a error,How I Know…
What row cannot inserted ?? or What number of row is wrong ??? I need send a message with this information.
Thanks.
Apologize for my bad english
Arthur,
Can you post some code.
Im not have problem with my code…mmmm
I have a excel file with two columns: Column1,Column2 and also have a table with 2 columns: Column1(int) and Column2(varchar(50))…
Imagine someone want to upload a excel file like this:
|Column1 | Column2 |
|1 |Info |
|2 |Info |
|a |info |
|4 |info |
Obviously this throw a error…. I need show a message like this:
The row 3 in your file is wrong, please correct it.
Thanks for everything!!!
Apologize for my bad english
Hi,
I happened to see your post find it quite informative. I would like to share a link where a software engineer has shared a tip on “SqlBulkCopy In .NET”.
Here is the link:
http://www.mindfiresolutions.com/SqlBulkCopy-In-NET-215.php
Hope you find it useful and of assistance.
Thanks,
Bijayani