方法1:To Perform a Transaction With a Shared Connection
using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope()) using (TransactionScope ts = new TransactionScope()) { Product p = new Product(1); p.Title = "new title";
Product p2 = new Product(2); p.Title = "another new title";
// ... p.Save(); p2.Save(); ts.Close(); }
方法2:添加引用 using System.Transactions; using (TransactionScope scope = new TransactionScope()) { try { OrderSellerList osl1 = new OrderSellerList(); osl1.OrderId = 9; osl1.SellerId = 2; osl1.UserId = 1; osl1.Save(); OrderSellerList osl2 = new OrderSellerList(); osl2.OrderId = 200; osl2.SellerId = 2; osl2.UserId = 1; osl2.Save(); scope.Complete(); } catch{} }
方法3:Simple Pass-through Transactions //create a new command collection
QueryCommandCollection coll = new QueryCommandCollection();
//change some data
Product p1 = new Product(1);
p1.ProductName = "New Name";
Product p2 = new Product(2);
p2.ProductName = "Another new name";
//add the commands to the collection
//by pulling them from the objects
//can also use GetInsertCommand() and GetDeleteCommand()
coll.Add(p1.GetUpdateCommand(Environment.UserName));
coll.Add(p2.GetUpdateCommand(Environment.UserName));
//Hand them to the DataService to execute in the scope
//of a transaction
DataService.ExecuteTransaction(coll);