.NET Tip of The Day
Learn one new .NET trick every day
Web GTD system from the creators of DotNetTipOfTheDay.org          Login or Join

Speed up inserting records into database with SqlBulkCopy class

When you need to insert a great deal of rows into database (for example, when importing data from a flat file or when importing data from one database into another one), and you need to do it programmatically because it needs to be pre-processed on the fly, then you should use SqlBulkCopy class.

The class uses Tabular Data Stream for fast transmitting data from client to a database server (therefore you'll see a bunch of "strange" INSERT BULK statements in a SQL Profiler):

    DataTable dataTable = new DataTable();

    ...

 

    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))

    {

        bulkCopy.DestinationTableName = "dbo.DestinationTale";

        bulkCopy.WriteToServer(dataTable);

    }

Also try different values for SqlBulkCopy.BatchSize to adjust performance for your case.

P.S. the tip applies only to MS SQL Server.

11/14/2007
RSS .NET Tip of The Day
Subscribe to receive one tip from the .NET Tips and Tricks Community per day.
Previous Tips of The Day
The best of the .NET Tips & Tricks Community.
.NET Practitioners .NET Tips & Tricks Community
Every .NET practitioner has a trick up in their sleeve. This is the place to share it with other .NET people.
Submit a Tip
Discovered a new trick? Share it with others.
My Tips
Manage tips you authored.