我是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
}
答案 0 :(得分:1)
File
中没有交易管理员,它不是"软件交易"。几乎100%的时间TransactionScope
将与ADO.NET或构建在其上的库(如Entity Framework或Dapper)结合使用。有关详情,请访问MSDN TransactionScope Class。