by admin
10. July 2008 06:50
Often there is a need to send a very large number of rows from business code to the database. There are a number of ways to do it:
- Call insert statements a row at a time for the entire data
- Serialize the data into some flat format (CSV or XML), send it to a stored procedure as a large string, unserialize the string in the stored procedure TSQL and do an insert.
- Save the data into a flat file format on the database server. Run a DTS package or like to read up the file.
- SqlBulkCopy!
Every since I discovered SqlBulkCopy, I’ve loved it. MS SQL Server includes a popular command called bcp for moving data from one table to another on one server or between servers. The SqlBulkCopy is a class that provides similar functionality.
SqlBulkCopy is way faster than multiple insert statements, serializing/ deserializing the data, or saving the data out to a file system and running an import. Its also has no limit on the data you can send across and very efficient in the way it handles inserts.
This is how simple it is to use it. In the example we have a function that writes copies are DataTable into a MS SQL database table called “tblFooBar”.
using System.Data.SqlClient;
…
Function WriteToDB(DataTable dt)
{
SqlBulkCopy sqlBC = new SqlBulkCopy(dbconnectionstring);
sqlBC.BatchSize = 25000;
sqlBC.BulkCopyTimeout = 60;
sqlBC.DestinationTableName = “dbo.tblFooBar” ;
sqlBC.WriteToServer(dt);
}
…
The MSDN link is:
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx