Bulk Insert with LINQ to SQL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bob laughland

    Bulk Insert with LINQ to SQL

    Hi All,

    I am using a combination of LINQ to SQL and bulk insert. In the
    process of performing 'one unit of work' I will be doing things like
    reading, and deleting records using LINQ to SQL and then inserting new
    records using bulk insert.

    One problem I am having is trying to use a 'transaction' to wrap
    around the whole thing.

    In the LINQ to SQL code I am doing this (basically), and this sorts
    out a transaction with all the LINQ to SQL code
    ,
    DataContext dataContext = new DataContext("co nstring"); // this is not
    exactly how I am doing it of course.
    dataContext.Con nection.Open();
    dataContext.Tra nsaction = dataContext.Con nection.BeginTr ansaction();

    Then in the Dispose method of my repository I am doing this,

    dataContext.Tra nsaction.Commit ();
    dataContext.Tra nsaction.Dispos e();
    dataContext.Con nection.Close() ;
    dataContext.Dis pose();

    And in the bulk insert code I am doing something like this,

    using (SqlConnection connection = new SqlConnection(" constring"))
    {
    connection.Open ();
    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(con nection))
    {
    bulkCopy.Destin ationTableName = databaseTableNa me;
    bulkCopy.WriteT oServer(dt);
    }
    }

    But the types of things don't match up. The connection opened up by
    LINQ to SQL is of type DBConnection, and the transaction is of type
    DBTransaction.

    The bulk insert code uses Connection and Transaction.

    How can I use the transaction I created across the bulk insert?

    I tried to use a TransactionScop e, but that caused other problems, but
    maybe this is an option I need to explore more?

    TransactionScop e was a problem because other SQL stataments are going
    to be running at the same time (for logging) and I didn't want them to
    be part of the transaction. I got some exceptions relating to that
    too.
    connection.Clos e();
Working...