How do I make SqlBulkCopy work with MS Enterprise Library?

enterprise-library sqlbulkcopy

Question

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"
};

Accepted Answer

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




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why