'ASP.Net database and IO operations in one transaction

I need to execute a series of file system and database operations in a .Net web application where if a failure occurs everything is rolled back. The operations are:

  1. Upload multipage tiff file to server using HttpPostedFile
  2. Insert record of posted file to database
  3. Process and save individual images in multipage tiff as individual files on server
  4. Insert record of individual images to database

Can I wrap these operations in a single transaction using the available classes in the 3.5 framework? Should I just use try catch blocks and rollback operations manually?



Solution 1:[1]

Try something like the following:

using (TransactionScope scope = new TransactionScope())
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        try
        { 
            // Do all work here...  
        }
        catch (Exception ex)
        {
           // Delete files
           // LogError(ex);
        }                   
    }
    scope.Complete();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1