|
|||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionSqlBulkCopy in ADO.NET 2.0 SqlBulkCopy
is a new feature in ADO.NET 2.0
that gives you DTS-like speeds when you need to programmatically copy
data from one database to another.That lets you to perform copy operation for a
large amount of data between a source data store and a destination data table. BackgroundI was
facing problem in my last application. I have to work on the data migration. It
was from Fox Pro to SQL server 2005.There are million of records in each table
and also other problem some of table contains more 150 Column. There was no
concept of normalization in the fox pro database I also have to manage that. I
come across SqlBulkCopy Class in Stem.Data.sqlcleint. This class
helps me solve my problem lots. So I have make mind to share the basic ideas of
this class with u people.Copying a
large amount of data from a source data store to a destination table in SQL Using the codeSqlBulkCopy SqlBulkCopy
is the object that helps you to perform a bulk copy. You can use a
DataReader or DataTable as source data store (you can load your data from SQL
database, Access database, XML or ... into these objects easily) and copy them
to a destination table in database. To accomplish this task, SqlBulkCopy uses a collection of SqlBulkCopyColumnMapping objects which will be saved as its SqlBulkCopyColumnMappingCollection property. SqlBulkCopyColumnMapping maps a column in data source to a table in destination table via their name or index. SqlBulkCopy has some important properties that you should be aware of them to be able to use it: BatchSize: This integer value specifies the
number of rows that should be copied in each attempt to copy to database.
This value has direct effect on the number of accesses to database. DestinationTableName: String value of destination
table's name. This object
also has four overloads. You can pass a SqlConnection (or a connection
string) plus an optional SqlBulkCopyOptions and SqlTransaction to its
constructor. Latest two parameters can change the behavior of SqlBulkCopy
object. Using SqlBulkCopyOptions enumerator you can specify that for
example SqlBulkCopy keeps identities or check constraints and some other
options. Using SqlTransaction you can pass an external SqlTransaction and
your SqlBulkCopy uses this transaction in all parts of the process. SqlBulkCopy
also has a SqlRowsCopied event handler that triggers when the specific
number of DataRows that have been copies. You specified this value via
NotifyAfter property. This handler is helpful when you want to be
aware of your process (for instance showing it via a ProgressBar to end
user). The last
thing that should be mentioned about SqlBulkCopy object is its WriteToServer()
method. This method can get an array of DataRows, a DataTable or a
DataReader and copies their content to destination table in database. SqlBulkCopyColumnMapping SqlBulkCopyColumnMapping
is the object that maps your source columns to destination columns in a bulk
copy. A SqlBulkCopyColumnMapping can get the source and destination
column names or ordinals via its properties or its constructor. It has
these properties: One of
SourceColumn and SourceOrdinal and one of DestinationColumn and
DestinationOrdinal should be set. Also you can set these properties via
constructor which is an easier way. Note that
if your source and destination columns have same names, it's not required to
use SqlBulkCopyColumnMapping objects because SqlBulkCopy can do its job
automatically. Let's have
look on example. I think the sample application you'll see in a moment
can cover all you need to use SqlBulkCopy. NOTE: If u have same table column on
both sides mean source and Destination. Then I will suggest that u shout not
mention SqlBulkCopyColumnMapping properties. Because it perform all tasks
automatically. PerformBulkCopy() method is used because both side column are same. MsoNormal"> Code public class mySqlBulkCopy { private static void PerformBulkCopy() { string connectionString = @"Server=localhost;Database=Northwind;Trusted_Connection=true"; // get the source data using (SqlConnection sourceConnection = new SqlConnection(connectionString)) { SqlCommand myCommand = new SqlCommand("SELECT * FROM Customer", sourceConnection); sourceConnection.Open(); SqlDataReader reader = myCommand.ExecuteReader(); // open the destination data using (SqlConnection destinationConnection = new SqlConnection(connectionString)) { // open the connection destinationConnection.Open(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection.ConnectionString)) { bulkCopy.BatchSize = 500; bulkCopy.NotifyAfter = 1000; bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(bulkCopy_SqlRowsCopied); bulkCopy.DestinationTableName = " Customer_Backup"; bulkCopy.WriteToServer(reader); } } reader.Close(); } } // this method is used if u want to mape column with different names // u can also use the index of column by using SourceOrdinal private static void PerformBulkCopyMyDifferentSchema() { string connectionString = @"Server= localhost;Database=Northwind;Trusted_Connection=true"; DataTable sourceData = new DataTable(); // get the source data using (SqlConnection sourceConnection = new SqlConnection(connectionString)) { SqlCommand myCommand = new SqlCommand("SELECT * FROM Customer", sourceConnection); sourceConnection.Open(); SqlDataReader reader = myCommand.ExecuteReader(); // open the destination data using (SqlConnection destinationConnection = new SqlConnection(connectionString)) { // open the connection destinationConnection.Open(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection.ConnectionString)) { bulkCopy.ColumnMappings.Add("CustomerID","ID"); bulkCopy.ColumnMappings.Add("CustomerName", "Name"); bulkCopy.ColumnMappings.Add("CustomerPoint", "Points"); bulkCopy.DestinationTableName = " CustomerPoints"; bulkCopy.WriteToServer(reader); } } reader.Close(); } } } }MsoNormal"> Summary If u are familiar with SQLHELPER class than u can improve ur performance more efficiently In this article I talked about newly added feature in ADO.NET 2.0, SqlBulkCopy, which helps you to bulk copy large amounts of data between data source and data table and improve your performance
|
||||||||||||||||||||||||||||||