new feature in ADO.NET version 2.0 :SqlBulkCopy

ADO.NET (ActiveX Data Object) is a set of classes used for data access in .NET. ADO allows .NET programmers to access a database without even

knowing how the database has been implemented. It is a part of base class library that is included with the Microsoft .NET Framework.
The simplest and fastest way of copying a large amount of data from different sources to an SQL Server table can be accomplished with the help of Bulk Copy Operation, a new feature in ADO.NET version 2.0.

Here different sources of data are those from which data can be loaded into 'DataTable' instance or read with 'IDataReader' instance. To accomplish bulk copy one has to use 'SqlBulkCopy' class that is present in 'Sytem.Data.SqlClient' namespace in .NET framework. Now using this 'SqlBulkCopy' class one can perform single bulk copy, multiple bulk copy and bulk copy with a transaction.

using System.Data.SqlClient;
string connectionString = "Data Source=.;Initial Catalog=NTL;User ID=sa;Password=P@ssw0rd";
SqlConnection sourceConnection = new SqlConnection(connectionString);
sourceConnection.Open();

SqlCommand commandSourceData = new SqlCommand("SELECT * FROM Source;", sourceConnection);
SqlDataReader reader = commandSourceData.ExecuteReader();
Connect to a destination table of data using:
SqlConnection destinationConnection = new SqlConnection(connectionString);
destinationConnection.Open();

SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection);
bulkCopy.DestinationTableName = "Destination";
bulkCopy.WriteToServer(reader);
reader.Close();



No comments:

Post a Comment