我有这个从通用 DAO 继承方法的特定 DAO 我想向此代码添加一个事务,以便回滚在发现异常时所做的所有更改
TDAO tDAO = new TDAO();
TDAO2 tDAO2 = new TDAO2();
//Get the DAO to delete from the database let's call them dDao and dDao2
//Start the Transaction
using (TransactionScope trans = new TransactionScope())
{
try
{
//Delete all SGC Associated switch
TDAO2.Delete(dDao);
//Delete switch
TDAO.Delete(dDao2);
//send notification
base.SendChangeNotification();
//Commit the Information Completing the Transaction
trans.Complete();
}
catch (UpdateException ex)//SQL exception
{
//Log the error
//Rollback the changes if there is an exception
throw new Exception(Resources.ErrorMessages.OperationNotPermited);
}
catch (Exception ex) //Other exception
{
//Log the error
throw new Exception(Resources.ErrorMessages.UndifenedError);
}
}
在 Visual Studio 中,转到项目中的“引用”图标。右键单击,添加引用。然后搜索 System.Transactions.dll。选择它,单击确定。然后尝试重建您的项目。还要确保顶部有 Using 语句 (C#) 或 Imports 语句 (VB),例如 Using System.Transactions;
并且更改在代码中。谢谢你