事物TransactionScope
using System.Transactions
如果修改、删除、添加一类操作涉及到多张表,那么最好用事物 trans.Complete();保证多张表的同时修改
using (TransactionScope trans = new TransactionScope()) { if (result.Result) { foreach (SubOrders subOrders in listSubOrders) { if (result.Result) { result.Result = _orderDao.UpdateSubOrders(subOrders) > 0; } } } if (result.Result) { OrderPaymentDetails orderPaymentDetails = new OrderPaymentDetails() { Amount = mainOrders.TotalPay, Bank = category.ToString(), CreateTime = DateTime.Now, MainOrderID = mainOrders.MOID, Method = 1, PayParam = string.Join("&", listParam) }; result.Result = _orderDao.InsertOrderPaymentDetails(orderPaymentDetails) > 0; } if (result.Result) { trans.Complete(); } }