c# TransactionScope没有回滚
本文关键字:TransactionScope | 更新日期: 2023-09-27 18:17:19
我是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
}
在File
中没有事务管理器,它不是"软件事务"。几乎100%的情况下,TransactionScope
将与ADO一起使用。. NET或在其基础上构建的库,如Entity Framework或Dapper。查看更多MSDN TransactionScope Class