[c#]两种Transaction和用法

[c#]两种Transaction和用法

 

http://kinghome1979.spaces.live.com/Blog/cns!BD659B93175DA3EA!239.entry

 

第一种:PetShopProfileProvider.cs中的用法
   SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringProfile);
   conn.Open();
   SqlTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);
   try {
    SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sqlDelete, param);
    SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sqlInsert, parms);
    trans.Commit();
   }
   catch(Exception e) {
    trans.Rollback();
    throw new ApplicationException(e.Message);
   }
   finally {
    conn.Close();
   }
第二种:MB.TheBeerHouse.BLL.Store.Order : BaseStore中的用法
         using (TransactionScope scope = new TransactionScope())
         {
            transactionID = BizObject.ConvertNullToEmptyString(transactionID);
            trackingID = BizObject.ConvertNullToEmptyString(trackingID);
            // retrieve the order's current status ID
            Order order = Order.GetOrderByID(id);
            // update the order
            OrderDetails record = new OrderDetails(id, DateTime.Now, "", statusID, "", "", 0.0m, 0.0m,
               "", "", "", "", "", "", "", "", "", "", shippedDate, transactionID, trackingID);
            bool ret = SiteProvider.Store.UpdateOrder(record);
           
            // if the new status ID is confirmed, than decrease the UnitsInStock for the purchased products
            if (statusID == (int)StatusCode.Confirmed && order.StatusID == (int)StatusCode.WaitingForPayment)
            {
               foreach (OrderItem item in order.Items)
                  Product.DecrementProductUnitsInStock(item.ProductID, item.Quantity);
            }
            BizObject.PurgeCacheItems("store_order");
            scope.Complete();
            return ret;
         }

你可能感兴趣的:(0016-005,.net)