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();
}
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.
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.
COMMIT: If everything is in order with all statements within a single transaction, all changes are recorded together in the database is called committed.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With