Esempio n. 1
0
        static void Listing18_4()
        {
            Console.WriteLine("{0} : Begin", new StackTrace(0, true).GetFrame(0).GetMethod().Name);

            TestDB db = new TestDB(@"Data Source=.\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=SSPI;");

            db.Shapes.InsertOnSubmit(new Square {
                Width = 4
            });
            db.Shapes.InsertOnSubmit(new Rectangle {
                Width = 3, Length = 6
            });
            db.Shapes.InsertOnSubmit(new Rectangle {
                Width = 11, Length = 5
            });
            db.Shapes.InsertOnSubmit(new Square {
                Width = 6
            });
            db.Shapes.InsertOnSubmit(new Rectangle {
                Width = 4, Length = 7
            });
            db.Shapes.InsertOnSubmit(new Square {
                Width = 9
            });

            db.SubmitChanges();

            Console.WriteLine("{0} : End", new StackTrace(0, true).GetFrame(0).GetMethod().Name);
        }
Esempio n. 2
0
        static void Listing18_6()
        {
            Console.WriteLine("{0} : Begin", new StackTrace(0, true).GetFrame(0).GetMethod().Name);

            Northwind db     = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;");
            TestDB    testDb = new TestDB(@"Data Source=.\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=SSPI;");

            Customer cust = db.Customers.Where(c => c.CustomerID == "LONEP").SingleOrDefault();

            cust.ContactName = "Barbara Penczek";

            Rectangle rect = (Rectangle)testDb.Shapes.Where(s => s.Id == 3).SingleOrDefault();

            rect.Width = 15;

            try
            {
                using (System.Transactions.TransactionScope scope =
                           new System.Transactions.TransactionScope())
                {
                    db.SubmitChanges();
                    testDb.SubmitChanges();
                    throw (new Exception("Just to rollback the transaction."));
                    //  A warning will result because the next line cannot be reached.
                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, cust);
            Console.WriteLine("Contact Name = {0}", cust.ContactName);

            testDb.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, rect);
            Console.WriteLine("Rectangle Width = {0}", rect.Width);

            Console.WriteLine("{0} : End", new StackTrace(0, true).GetFrame(0).GetMethod().Name);
        }