TransactionScope transaction operations

  using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    InsertUserBase (); // it is inserted unsuccessful, to roll back their 

                    UserInfos the userInfo = new new UserInfos
                    {
                        UserID = "1",
                        RealName = "zzl",
                    };
                    db.UserInfos.InsertOnSubmit(userInfo);
                    db.SubmitChanges();

                    trans.Complete();
                }
                catch (Exception)
                {

                    // throw;
                }
                finally
                {
                    trans.Dispose ();
                }
            }
            #endregion

  static bool InsertUserBase()
        {

            bool flag;
            try
            {
                UserBases userbase = new UserBases
                {
                    UserID = "0005",
                    Name = "zzl",
                    CreateDate = DateTime.Now,
                    UpdateDate = DateTime.Now,

                };
                db.UserBases.InsertOnSubmit(userbase);
                db.SubmitChanges();
                flag = true;
            }
            catch (Exception)
            {

                throw;
            }
            return flag;

        }

InsertUserBase () as long as the abnormal, the program will roll back their own

 

Guess you like

Origin www.cnblogs.com/shy1766IT/p/12160838.html