使用事务的方法示范

public static bool AddPro(string strSKU,int intCategoryID,string strName,int intBrandID,string strKeywords,string strIntroduce,string strDescription,decimal Price,decimal Price3,decimal Price5,decimal Price10,decimal PriceMarket,int intIsNew,int intIsPop,int intIsPromotion,int intIsOnsale,int intIsOut,int intWeight,string strPackage,string strSize,int intPType,string strSpeciaOfferNote)
        {
            //使用事务方法示范
            using (SqlConnection conn = SqlHelper.GetConnection())
            {
                conn.Open();
                SqlTransaction transaction = conn.BeginTransaction("MyTransaction");
                try
                {
                    ProductEntity proEntity = new ProductEntity();
                    proEntity.SKU = strSKU;
                    proEntity.Name = strName;
                    proEntity.bPic = false;
                    proEntity.BrandID = intBrandID;
                    proEntity.Keywords = strKeywords;
                    proEntity.Introduce = strIntroduce;
                    proEntity.Description = strDescription;
                    proEntity.Price = Price;
                    proEntity.Price3 = Price3;
                    proEntity.Price5 = Price5;
                    proEntity.Price10 = Price10;
                    proEntity.PriceMarket = PriceMarket;
                    proEntity.IsNew = intIsNew;
                    proEntity.IsPop = intIsPop;
                    proEntity.IsPromotion = intIsPromotion;
                    proEntity.IsOnsale = intIsOnsale;
                    proEntity.IsOut = intIsOut;
                    proEntity.Weight = intWeight;
                    proEntity.Package = strPackage;
                    proEntity.Size = strSize;
                    proEntity.bStorage = false;
                    proEntity.bStorageWar = false;
                    proEntity.bRank = false;
                    proEntity.bDownloads = false;
                    proEntity.bSellnum = false;
                    proEntity.AddDate = DateTime.Now;
                    proEntity.bEditDate = false;
                    proEntity.PType = intPType;
                    proEntity.SpecialOfferNotes = strSpeciaOfferNote;
                    proEntity.Add(transaction);

                    ProductCategoryEntity productcategory = new ProductCategoryEntity();
                    productcategory.SKU = strSKU;
                    productcategory.OrderIndex = 1;
                    productcategory.CategoryID = intCategoryID;
                    productcategory.Add(transaction);

                    transaction.Commit();
                    return true;
                }
                catch
                {
                    try
                    {
                        transaction.Rollback();//(事务回滚)
                         //写日志

                    }
                    catch
                    {
                        //(事务回滚失败)

         //写日志
                    }
                    return false;
                }
            }

 

            //Silver:建议的事务使用方式。其实实现事务的方法更好的方法TransactionScope类,事务可以跨数据库。
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    SqlConnection conn = SqlHelper.GetConnection();
                    conn.Open();
                    ProductEntity proEntity = new ProductEntity();
                    proEntity.SKU = strSKU;
                    proEntity.Name = strName;
                    proEntity.bPic = false;
                    proEntity.BrandID = intBrandID;
                    proEntity.Keywords = strKeywords;
                    proEntity.Introduce = strIntroduce;
                    proEntity.Description = strDescription;
                    proEntity.Price = Price;
                    proEntity.Price3 = Price3;
                    proEntity.Price5 = Price5;
                    proEntity.Price10 = Price10;
                    proEntity.PriceMarket = PriceMarket;
                    proEntity.IsNew = intIsNew;
                    proEntity.IsPop = intIsPop;
                    proEntity.IsPromotion = intIsPromotion;
                    proEntity.IsOnsale = intIsOnsale;
                    proEntity.IsOut = intIsOut;
                    proEntity.Weight = intWeight;
                    proEntity.Package = strPackage;
                    proEntity.Size = strSize;
                    proEntity.bStorage = false;
                    proEntity.bStorageWar = false;
                    proEntity.bRank = false;
                    proEntity.bDownloads = false;
                    proEntity.bSellnum = false;
                    proEntity.AddDate = DateTime.Now;
                    proEntity.bEditDate = false;
                    proEntity.PType = intPType;
                    proEntity.SpecialOfferNotes = strSpeciaOfferNote;
                    proEntity.Add(conn);

                    ProductCategoryEntity productcategory = new ProductCategoryEntity();
                    productcategory.SKU = strSKU;
                    productcategory.OrderIndex = 1;
                    productcategory.CategoryID = intCategoryID;
                    productcategory.Add(conn);

                    conn.Close();//必须要申明这句,否则执行ts.Complete()会引发异常
                    ts.Complete();
                    return true;
                }
            }
            catch
            {
                //(事务自动回滚)

      //写日志

                return false;
            }

        }

posted @ 2009-11-29 00:37  Silver.Lee  阅读(458)  评论(0编辑  收藏  举报