Bulk Inserter

ronnieoverby edited this page Jul 6, 2012 · 3 revisions
Clone this wiki locally

The BulkInserter<T> class will bulk insert collections of any kind of object as long as the property names match the column names.

The class supports customizing the batch size, pre & post insert events, queued inserts, and "firehose mode" (give it a billion objects, it will respect the batch size).

Here's an example of "firehose mode," where BulkInserter will enumerate for you:

public void InsertPeople(SqlConnection sqlConnection, IEnumerable<Person> bazillionPeople)
{
    var tableName = "People";
    var bufferSize = 5000;
    var inserter = new BulkInserter<Person>(sqlConnection, tableName, bufferSize);
    inserter.Insert(bazillionPeople);
}

And "leaky faucet" mode, for when you need to enumerate the objects before inserting them:

public void InsertPeople(SqlConnection sqlConnection, IEnumerable<Person> bazillionPeople)
{
    var tableName = "People";
    var bufferSize = 5000;
    var inserter = new BulkInserter<Person>(sqlConnection, tableName, bufferSize);

    foreach(var person in bazillionPeople)
        inserter.Insert(person); // this will bulk insert when buffer size is reached

    inserter.Flush(); // bulk insert the last few
}

If you need to do something before or after each bulk insert, just wire up these events:

var inserter = new BulkInserter<Person>(sqlConnection, tableName, bufferSize);
inserter.PreBulkInsert += PreBulkInsertHandler;
inserter.PostBulkInsert += PostBulkInsertHandler;

The items that are being inserted will be passed in the event args.