using (TransactionScope trans = new TransactionScope()) { try { InsertUserBase(); //它插入不成功,自己回滚 UserInfos userInfo = 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()只要出现异常,程序将自己回滚
原文地址:https://www.cnblogs.com/shy1766IT/p/12160838.html
时间: 2024-11-15 00:24:11