Search code examples
c#-4.0transactionstransactionscopestrongly-typed-dataset

TransactionScope with Typed Dataset


Is it possible to use TransactionScope with a Typed Dataset?

as in:

using (var transaction = new TransactionScope())
{
    typedDataSet.DeleteStuff(id);

    typedDataSet2.DeleteSomeOtherStuff(id2);

    transaction.Complete();
}

Will the the sql queries related to DeleteStuff(id) and DeleteSomeOtherStuff(id) actually be transactional if an error is thrown?

I have read this article by Bogdan Chernyachuk on Using Transactions with Strongly Typed datasets and I am hoping that I do not have to do it this way.


Solution

  • Short answer: Yes this is transactional.

    Wasn't too hard to test either. I threw an exception just before the transaction.Complete() and the data wasn't deleted from the database.

    using (var transaction = new TransactionScope())
    {
        typedDataSet.DeleteStuff(id);
    
        typedDataSet2.DeleteSomeOtherStuff(id2);
        throw new NullReferenceException();
        transaction.Complete();
    }
    

    Weirdly though I profiled what was going on in the db with SQL SERVER Profiler and the stored procedures that were referenced through the typed dataset were executed on the server. However the data was somehow rolled back.