Introduction
This article explains how to copy data of one table to another table using bulk copy. In this example I explain a scenario when both tables are in two different databases on the same server. The table where the data is being copied from is the source table and the table in which the data is being copied into is called the destination table. The copy data is being done from a Web application and the code is written in C# and ADO.NET.
Step 1. Source table with records
First of all, create a new database table by executing the following scripts in one database and insert some data into it. The following script creates a new table, Products with 100,000 rows.
- Create Table Products
- (
- [Id] int primary key,
- [Name] nvarchar(50),
- [Desc] nvarchar(250)
- )
- GO
- Declare @Id int
- Set @Id = 1
- While(@Id <= 100000)
- Begin
- Insert into Products values
- (@Id, 'This Is Product No - ' + CAST(@Id as nvarchar(20)),
- 'It Contains Total Product - ' + CAST((@Id+10) as nvarchar(20)))
- Print @Id
- Set @Id = @Id + 1
- End
Step 2. Empty destination table
Now we create another database and create the same table structure of table in that databse.
- Create Table Products
- (
- [Id] int primary key,
- [Name] nvarchar(50),
- [Desc] nvarchar(250)
- )
Note: If you want you can create different structure and different columns, you can do that as well. Just name sure the data type of the columns are same for both source and destination tables.
Step 3. Create a Web application
Now open Visual Studio and create an empty Web project. In web.config file, create two connection string like the following:
- <connectionStrings>
- <add name="Source" connectionString="Data Source=*****; Initial Catalog=manish_db;
- User Id=sa; password=*****" providerName="System.Data.SqlClient"/>
- <add name="Destination" connectionString="Data Source=*****; Initial Catalog=TestDB;
- User Id=sa; password=*****" providerName="System.Data.SqlClient"/>
- </connectionStrings>
Here Source connection string is for the first database and the Destination connection string is for the second database. We will read data from the Products table of the source database and copy into the Products table of the destination database.


Harsh PatelPosted Apr 5, 2022, 7:27 AM
Very Helpfull!
Pankajkumar PatelPosted Aug 19, 2019, 12:35 AM
Nice once ...
shOckz FringePosted Sep 6, 2016, 1:50 AM
Nice! Thank you for sharing. will this work from SQL server to Amazon Redshift?
Kamlesh TiwariPosted Mar 15, 2016, 12:50 AM
Nice Artical, thanks for sharing !!!
shravan kumarPosted Mar 18, 2015, 2:29 AM
nice article. thanq for sharing...