Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make SqlBulkCopy work with MS Enterprise Library?

I've got some code which uses SqlBulkCopy. And now we're refactoring our code to use Enterprise Library database functions instead of standard ones. The question is how can I instantiate SqlBulkCopy? It accepts SqlConnection, and I only have DbConnection.

var bulkCopy = new SqlBulkCopy(connection)  // here connection is SqlConnection
{
    BatchSize = Settings.Default.BulkInsertBatchSize,
    NotifyAfter = 200,
    DestinationTableName = "Contacts"
};
like image 805
HiveHicks Avatar asked Jul 28 '10 17:07

HiveHicks


1 Answers

Really easy, we use it like that and it works perfectly :

using (DbConnection connection = db.CreateConnection())
{
    connection.Open();
    //blah blah

    //we use SqlBulkCopy that is not in the Microsoft Data Access Layer Block.
    using (SqlBulkCopy copy = new SqlBulkCopy((SqlConnection) connection, SqlBulkCopyOptions.Default, null))
    {
        //init & write blah blah
    }
}

The solution is to cast the connection : (SqlConnection) connection

like image 81
Julien N Avatar answered Sep 30 '22 18:09

Julien N