This documentation is archived and is not being maintained.

How to: Bracket Data Submissions by Using Transactions (LINQ to SQL)

You can use TransactionScope to bracket your submissions to the database. For more information, see Transactions (LINQ to SQL).

The following code encloses the database submission in a TransactionScope.


            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();
		    ts.Complete();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }


Show: