0

I want to create a file and persist some records in DB (SQL server) together in one transaction. So "creating a file and inserting records into DB" must happen both or none. I tried below code but it didn't work:

using (connectionDb)
{
    connectionDb.Open();
    using (var ts = new System.Transactions.TransactionScope())
    {
        try
        {
            File.Copy(sourceFileName, destFileName, overwrite);
            connectionDb.ExecuteNonQuery();
            ts.Complete();
        }
        catch (Exception)
        {
            throw;
        }
        finally
        { }
    }
}

I've studied about Transactional NTFS (TxF), but I don't know how I can work with it and if it is helpful or not. I use SQL server 2008 R2 and windows 7/server 2008 R2.

Pegah
  • 1
  • 1
  • 3
    You should understand that a transaction is only a database-operation, you cannot save a file there. You have to wrap both operations into a block and if saving the file fails rollback the transaction. – MakePeaceGreatAgain Oct 28 '15 at 12:50
  • 1
    I presume you've seen this: http://stackoverflow.com/questions/28215267/how-to-transaction-a-io-operation-and-a-database-execution – MattC Oct 28 '15 at 12:53

0 Answers0