ADO.NET tutorial for beginners - Part 19 Copying data from one table to another table using SqlBulkCopy

Database, Information Technology

ADO.NET tutorial for beginners

61 Lessons

Part 19 Copying data from one table to another table using SqlBulkCopy

In this video we will discuss copying data from one table to another table. The source and destination tables may be in the same database or in different databases and these database can be on the same sql server or in different servers. In Part 18 we discussed, loading xml data into sql server table using sqlbulkcopy. We will be continuing with the example we worked with in Part 18.

Step 1 : Create a new database. Name it SourceDB. Execute the following sql script to create Departments and Employees tables, and to populate with data.
Create table Departments
(
ID int primary key identity,
Name nvarchar(50),
Location nvarchar(50)
)
GO

Create table Employees
(
ID int primary key identity,
Name nvarchar(50),
Gender nvarchar(50),
DepartmentId int foreign key references Departments(Id)
)
GO

Insert into Departments values (‘IT’, ‘New York’)
Insert into Departments values (‘HR’, ‘London’)
Insert into Departments values (‘Payroll’, ‘Muumbai’)
GO

Insert into Employees values (‘Mark’, ‘Male’, 1)
Insert into Employees values (‘John’, ‘Male’, 1)
Insert into Employees values (‘Mary’, ‘Female’, 2)
Insert into Employees values (‘Steve’, ‘Male’, 2)
Insert into Employees values (‘Ben’, ‘Male’, 3)
GO

Step 2 : Create another new database. Name it DestinationDB. Execute the sql script to just create Departments and Employees tables. Here we have just the structre of the tables and no data. We will be moving data from SourceDB tables to DestinationDB tables.

Step 3 : Include the following 2 connection strings for the Source and Destination databases in the web.config file of the Demo project we created in Part 18.
[connectionStrings]
[add name=”SourceCS” connectionString=”server=.;database=SourceDB;Integrated Security=True”
providerName=”System.Data.SqlClient” /]
[add name=”DestinationCS” connectionString=”server=.;database=DestinationDB;Integrated Security=True”
providerName=”System.Data.SqlClient” /]
[/connectionStrings]

Step 4 : Copy and paste the following code in the button click event handler method in the code-behind file
string sourceCS = ConfigurationManager.ConnectionStrings[“SourceCS”].ConnectionString;
string destinationCS = ConfigurationManager.ConnectionStrings[“DestinationCS”].ConnectionString;
using (SqlConnection sourceCon = new SqlConnection(sourceCS))
{
SqlCommand cmd = new SqlCommand(“Select * from Departments”, sourceCon);
sourceCon.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
using (SqlConnection destinationCon = new SqlConnection(destinationCS))
{
using (SqlBulkCopy bc = new SqlBulkCopy(destinationCon))
{
bc.DestinationTableName = “Departments”;
destinationCon.Open();
bc.WriteToServer(rdr);
}
}
}
cmd = new SqlCommand(“Select * from Employees”, sourceCon);
using (SqlDataReader rdr = cmd.ExecuteReader())
{
using (SqlConnection destinationCon = new SqlConnection(destinationCS))
{
using (SqlBulkCopy bc = new SqlBulkCopy(destinationCon))
{
bc.DestinationTableName = “Employees”;
destinationCon.Open();
bc.WriteToServer(rdr);
}
}
}
}

Note: Please make sure to include the following using declarations
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

As the column names in the source and destination tables are the same column mappings are not required.

Text version of the video
http://csharp-video-tutorials.blogspot.com/2014/09/part-19-copying-data-from-one-table-to.html

Slides
http://csharp-video-tutorials.blogspot.com/2014/09/part-19-copying-data-from-one-table-to_24.html

All ADO .NET Text Articles
http://csharp-video-tutorials.blogspot.com/p/free-adonet-video-tutorial.html

All ADO .NET Slides
http://csharp-video-tutorials.blogspot.com/p/adonet-slides.html

All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists

    .Net .net sqldatareader multiple result sets Access Added ado net sql transaction example ado.net ado.net cache dataset ado.net transaction c# ado.net transaction example ado.net tutorial Advantages asp net dataset delete row asp net dataset update to database asp.net asp.net bind dataset multiple tables asp.net c# data access tutorial asp.net c# database connection asp.net c# database connection with sql server asp.net c# sqlbulkcopy asp.net connect to sql server database asp.net connect to sql server tutorial asp.net database application asp.net datareader multiple results asp.net dataset insert row asp.net dataset multiple tables asp.net dataset update row asp.net fill dataset with multiple tables asp.net gridview dataset multiple tables asp.net sql connection example c# asp.net sqlbulkcopy datatable asp.net transaction example asp.net web application database benefits bulk copy class c# C# c# .net data access tutorial c# bank transaction c# bulkcopy datatable c# data access tutorial c# data provider c# database transaction rollback c# datagridview dataset multiple tables c# datareader multiple tables c# dataset 2 tables c# dataset insert into database c# dataset insert update delete c# dataset multiple tables stored procedure c# dataset two tables c# dataset without database c# datatable rowstate deleted c# disconnected dataset c# load data from database c# sql transaction try catch c# sqlbulkcopy batch c# sqlbulkcopy class c# sqlbulkcopy database c# sqlbulkcopy datatable example c# sqlbulkcopy example c# sqlbulkcopy xml data c# sqlcommandbuilder update c# sqlconnection example c# sqlconnection sqlcommand c# sqldatareader example c# sqldatareader sample code c# transaction best practices c# transaction commit rollback c# transaction exception c# transaction rollback exception caching dataset calling can we store multiple tables in datareader class command commandbuilder update command c# configuration file connection connection string connection string sql authentication connectionstring windows authentication c# ConnectionStrings Data data access in dot net data access techniques in c# DataAdapter database datareader c# multiple result sets DataRowVersion dataset dataset crud example dataset disconnected architecture Deleted Detached difference between typed and untyped dataset difference between typed and untyped dataset in asp.net differences disconnected disconnected data access disconnected database in c# dot net dot net data provider dotnet example ExecuteNonQuery ExecuteReader ExecuteScalar executing gridview using dataset in asp.net gridview with dataset c# haschanges injection insert data using sqlbulkcopy in c# insert xml data into sql table using c# interview introduction to data access in .net load xml data into sql server methods Modified moving data from one table to another in sql multiple tables in datareader c# NextResult() method not working Object output parameters parameterized queries prevention primary key questions rejectchanges retrieve connection string from app.config c# retrieving multiple result sets using nextresult c# row RowState sql sql connection SQL Injection sql injection attack mechanisms and prevention techniques sql injection demo sql injection detection and prevention techniques sql injection for dummies sql injection forms sql injection get request sql injection hacking basics tutorial sql injection how to sql injection in search box sql injection in search field sql injection in search query sql injection lessons sql injection on website sql injection post request sql injection prevention asp.net sql injection prevention best practices sql injection prevention cheat sheet sql injection prevention in asp.net sql injection prevention methods sql injection prevention system sql injection prevention techniques sql injection prevention tutorial sql injection queries sql injection real life example sql injection real world example sql injection simple explanation sqlbulkcopy c# sqlbulkcopy example sqlbulkcopy table to table SqlCommand SqlCommandBuilder sqlcommandbuilder update c# sqlcommandbuilder update command c# sqlconnection sqldataadapter sqldataadapter c# example select SqlDataReader sqldatareader example sqldatareader loop through columns sqldatareader loop through fields sqldatareader loop through rows sqldatareader multiple tables sqldatareader read all columns sqldatareader read all rows sqldatareader read column value sqldatareader read example sqldatareader read to end sqldatareader read while states stored procedure stored procedures Strongly transaction in ado.net c# with example transaction in ado.net using c# transaction in asp.net transaction in asp.net c# transactions in ado.net tutorial two or more result sets typed typed versus untyped datasets typed vs untyped dataset Unchanged untyped Update use of using keyword in c# versions Weakly web application Web.Config what is data provider in c# windows application app config connection string