c#TransactionScope没有回滚

时间:2016-11-11 06:57:50

标签: c# file rollback transactionscope

我是C#的新手,并试用了TransactionScope的工作原理。这是我的代码,我想知道为什么我的交易没有回滚:

string file1 = "txf1.txt";
string file2 = "txf2.txt";
using (StreamWriter sw = File.CreateText(file1))
{
    sw.WriteLine("Hello World");
}

using (StreamWriter sw = File.CreateText(file2))
{
    sw.WriteLine("Hello World");
}

using (TransactionScope scope = new TransactionScope())
{
    File.AppendAllText(file1, "Transaktion");
    scope.Complete();
}

using (TransactionScope scope = new TransactionScope())
{
    File.AppendAllText(file2, "Transaktion");
    //should roll back the file, but doesn't
}

1 个答案:

答案 0 :(得分:1)

File中没有交易管理员,它不是"软件交易"。几乎100%的时间TransactionScope将与ADO.NET或构建在其上的库(如Entity Framework或Dapper)结合使用。有关详情,请访问MSDN TransactionScope Class