HOW TO:使用交易括住資料提交 (LINQ to SQL)
更新: November 2007
您可以使用 TransactionScope 來括住資料庫提交。如需詳細資訊,請參閱交易 (LINQ to SQL)。
範例
下列程式碼會將資料庫提交封入 TransactionScope 中。
Dim db As New Northwnd("c:\northwnd.mdf")
Using ts = New TransactionScope()
Try
Dim prod1 = db.Products.First(Function(p) p.ProductID = 4)
Dim prod2 = db.Products.First(Function(p) p.ProductID = 5)
prod1.UnitsInStock -= 3
prod2.UnitsInStock -= 5
db.SubmitChanges()
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Using
Northwnd db = new Northwnd(@"c:\northwnd.mdf");
using (TransactionScope ts = new TransactionScope())
{
try
{
Product prod1 = db.Products.First(p => p.ProductID == 4);
Product prod2 = db.Products.First(p => p.ProductID == 5);
prod1.UnitsInStock -= 3;
prod2.UnitsInStock -= 5;
db.SubmitChanges();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}