I have a problem, for saving a file and inserting a record in DB in a TransactionScope; Means saving file and inserting record, must depend together = or both or neither. Can anybody help me please?
问题:
回答1:
try
{
// Start DB Transaction
// Save To DAtabase code
// Save To File Code
// Commit DB Transaction
}
catch
{
// Rollback DB Transaction
}
Please notice Sequence of DB should be first then Saving to the file.
回答2:
Transactional NTFS
One of the coolest parts about Transactional NTFS is that it can work with a large number of other transactional technologies. Because TxF uses the new Kernel Transaction Manager (KTM) features, and because the new KTM can work directly with the Microsoft® Distributed Transaction Coordinator (DTC), any technology that can work with DTC as a transaction coordinator can use transacted file operations within a single transaction. This means that you can now enlist transacted file operations within the same transaction as SQL operations, Web service calls via WS-AtomicTransaction, Windows Communication Foundation services via the OleTransactionProtocol, or even transacted MSMQ operations.
MSDN link
Alpha FS provides Transaction NTFS in .NET. see Alphaleonis.Win32.Filesystem.KernelTransaction(Transaction transaction). You can get the current transaction by Transaction.Current
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
{
//KernelTransaction is in AlphaFS
KernelTransaction kt = new KernelTransaction(Transaction.Current);
//Append "hello" to text file named "text.txt"
Alphaleonis.Win32.Filesystem.File.WriteAllText(kt, "text.txt", "hello");
//No text appended because exception will be thrown
throw new Exception("oops");
ts.Complete();
}