What is the best way to get SqlBulkCopy to function 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"
};
1
5
11/10/2015 1:24:57 AM

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

8
7/29/2010 10:04:41 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow