Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a transaction to cover Moving a file and Inserting record in database?

Tags:

c#

asp.net

I want to have a transaction for copying a file and then inserting a record in database. something like below statement, but transaction doesn't cover copying file. What's the solution?

using (TransactionScope scope1 = new TransactionScope())
{
    // Copy a file
    fileMgr.Move(srcFileName, destFileName);

    // Insert a database record
    dbMgr.ExecuteNonQuery(insertSql);

    scope1.Complete();
}
like image 586
Raymond Morphy Avatar asked Oct 29 '11 13:10

Raymond Morphy


People also ask

Why do we need transaction in database?

Benefits of Transactions Many database uses require storing data to multiple tables, or multiple rows to the same table in order to maintain a consistent data set. Using transactions ensures that other connections to the same database see either all the updates or none of them.

What is TransactionScope in C#?

The TransactionScope class provides a simple way to mark a block of code as participating in a transaction, without requiring you to interact with the transaction itself. A transaction scope can select and manage the ambient transaction automatically.

Which of the following clauses is used to name all the changes made so far in a transaction?

COMMIT: If everything is in order with all statements within a single transaction, all changes are recorded together in the database is called committed.


1 Answers

Try to use .NET Transactional File Manager

This library allows you to wrap file system operations in transactions like this:

// Wrap a file copy and a database insert in the same transaction
TxFileManager fileMgr = new TxFileManager();
using (TransactionScope scope1 = new TransactionScope())
{
    // Copy a file
    fileMgr.Copy(srcFileName, destFileName);

    // Insert a database record
    dbMgr.ExecuteNonQuery(insertSql);

    scope1.Complete();
} 
like image 161
Damith Avatar answered Sep 30 '22 15:09

Damith