Exemplo n.º 1
0
        /// <summary>
        /// Using Transaction Scope with Exception Example
        /// </summary>
        private void Transactions3()
        {
            var dc = new NorthwindDataContext();

            var custId = "ALFKI";

            //Retrieve Customer
            var customer = dc.Customers.SingleOrDefault(c => c.CustomerID == custId);

            if (customer == null)
            {
                string.Format("Customer: [{0}] does NOT exist!").DisplayResults();
                return;
            }

            AddOrderResult newOrderResult = null;

            //Open transaction Scope
            using (var transScope = new System.Transactions.TransactionScope())
            {
                try
                {
                    //Create Customer Order
                    var result = dc.AddOrder(custId,
                                             4,
                                             DateTime.Today,
                                             DateTime.Today.AddDays(7),
                                             null,
                                             1,
                                             20.5m,
                                             "Costa Concordia",
                                             customer.Address,
                                             customer.City,
                                             customer.Region,
                                             customer.PostalCode,
                                             customer.Country).ToList();

                    newOrderResult = result[0];

                    //Create Details
                    dc.AddOrderDetail(newOrderResult.OrderID, 7, 25, 10, 0);
                    dc.AddOrderDetail(newOrderResult.OrderID, 99, 10, 15, 0);
                    dc.AddOrderDetail(newOrderResult.OrderID, 12, 20, 25, 0);

                    //Commit Transaction
                    transScope.Complete();
                }
                catch (Exception ex)
                {
                    "Exception".DisplayHeader();
                    ex.Message.DisplayResults();
                }
            }


            //Show Customer Orders
            Display.ShowCustomerOrders(dc, custId);
            //Show Order Details
            Display.ShowOrderDetails(dc, newOrderResult.OrderID);
        }