如何在C#中保存文件和在DB中插入记录之间创建一个Transactions望远镜

jav*_*iry 2 c# file-io dependencies transactions insert

我有一个问题,用于保存文件并在TransactionScope中的DB中插入记录; 意味着保存文件和插入记录,必须依赖于=或两者兼有.有人能帮帮我吗?

Los*_*ter 6

交易NTFS

Transactional NTFS最酷的部分之一是它可以与大量其他事务技术一起使用.由于TxF使用新的内核事务管理器(KTM)功能,并且因为新的KTM可以直接与Microsoft®分布式事务处理协调器(DTC)一起使用,所以可以使用DTC作为事务协调器的任何技术都可以使用事务协调器中的事务处理文件操作.单笔交易.这意味着您现在可以在与SQL 操作相同的事务中进行事务处理,通过WS-AtomicTransaction进行Web服务调用,通过OleTransactionProtocol进行Windows Communication Foundation服务,甚至是事务处理的MSMQ操作.

MSDN链接

Alpha FS在.NET中提供事务NTFS.请参阅Alphaleonis.Win32.Filesystem.KernelTransaction(事务处理事务).您可以通过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();
}
Run Code Online (Sandbox Code Playgroud)