Bulk Copy Using ADO.Net Sql Client
The Namespace System.Data.SqlClient's SqlBulkCopy class is used to copy the data from the one SQl Server table To another other table in the same or another database.
The Metod "WriteToServer" is used to write the source data to destination table.
The Bellow C# Class used for Bulk Copy
public class CopyData
{
string _sourceConnectionString;
string _destinationConnectionString;
public CopyData(string sourceConnectionString,string destinationConnectionString)
{
_sourceConnectionString = sourceConnectionString;
_destinationConnectionString = destinationConnectionString;
}
public void CopyTable(string table)
{
using (SqlConnection source = new SqlConnection(_sourceConnectionString))
{
string sql = string.Format("SELECT * FROM [{0}]",table);
SqlCommand command = new SqlCommand(sql, source);
source.Open();
IDataReader dr = command.ExecuteReader();
using (SqlBulkCopy copy = new SqlBulkCopy(_destinationConnectionString))
{
copy.DestinationTableName = table;
copy.WriteToServer(dr);
}
}
}
}
//To using the bellow code we can create object and call the buk copy method with the table name
CopyData c = new CopyData("Server=server;Database=Northwind;Integrated Security=SSPI", "Server=server;Database=master;Integrated Security=SSPI");
c.CopyTable("Orders");



1 comments:
Post a Comment