2016-11-11 72 views
0

我是C#中的新手,並嘗試瞭如何使用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

沒有在File一個事務管理器,它是不是一個「軟件交易」。幾乎100%的時間TransactionScope將與ADO.NET或基於其構建的庫(如Entity Framework或Dapper)一起使用。有關詳細信息,請參閱MSDN TransactionScope Class