/// <summary> /// 添加 /// </summary> /// <param name="info"></param> /// <returns></returns> public int Create(TB_RoleConnectModule info) { try { Database db = DBO.CreateDatabase(); return(this.Create(info, (DbTransaction)null, db)); } catch (Exception ex) { throw ex; } }
/// <summary> /// 事务添加 /// </summary> /// <param name="info"></param> /// <param name="tran"></param> /// <param name="db"></param> /// <returns></returns> public int Create(TB_RoleConnectModule info, DbTransaction tran, Database db) { DbCommand dbCommandWrapper = null; int intId; string strInsertSql = @"INSERT INTO [TB_RoleConnectModule] ([RoleID] ,[ModuleID]) VALUES(@RoleID ,@ModuleID )"; string strSelectIdSql = ";SELECT SCOPE_IDENTITY()"; try { dbCommandWrapper = db.GetSqlStringCommand(strInsertSql + strSelectIdSql); #region Add parameters db.AddInParameter(dbCommandWrapper, "@RoleID", DbType.Int32, info.RoleID); db.AddInParameter(dbCommandWrapper, "@ModuleID", DbType.Int32, info.ModuleID); #endregion if (tran == null) { intId = Convert.ToInt32(db.ExecuteScalar(dbCommandWrapper)); } else { intId = Convert.ToInt32(db.ExecuteScalar(dbCommandWrapper, tran)); } return(intId); } catch (Exception ex) { throw ex; } finally { if (dbCommandWrapper != null) { dbCommandWrapper = null; } } }
/// <summary> /// 更新角色信息 /// </summary> /// <param name="tb_Role"></param> /// <param name="lstModuleID"></param> private void Update(TB_Role tb_Role, List <int> lstModuleID) { TB_RoleConnectModule mTB_RoleConnectModule = null; //启用事务 _db = DBO.CreateDatabase(); _connection = _db.CreateConnection(); _connection.Open(); _tran = _connection.BeginTransaction(); try { //更新角色信息 _mTB_RoleDAL.Edit(tb_Role, _tran, _db); //删除角色关联模块信息 _mTB_RoleConnectModuleDAL.Delete(tb_Role.ID, _tran, _db); //添加角色关联模块信息 foreach (var item in lstModuleID) { mTB_RoleConnectModule = new TB_RoleConnectModule() { RoleID = tb_Role.ID, ModuleID = item, }; _mTB_RoleConnectModuleDAL.Create(mTB_RoleConnectModule, _tran, _db); } //提交事务 _tran.Commit(); } catch { //回滚事务 _tran.Rollback(); } finally { //关闭连接 _connection.Close(); } }
/// <summary> /// 添加角色信息 /// </summary> /// <param name="tb_Role"></param> /// <param name="lstModuleID"></param> /// <returns></returns> private int Add(TB_Role tb_Role, List <int> lstModuleID) { TB_RoleConnectModule mTB_RoleConnectModule = null; int intRoleID = 0; //启用事务 _db = DBO.CreateDatabase(); _connection = _db.CreateConnection(); _connection.Open(); _tran = _connection.BeginTransaction(); try { //添加角色信息 intRoleID = _mTB_RoleDAL.Create(tb_Role, _tran, _db); //添加角色关联模块信息 foreach (var item in lstModuleID) { mTB_RoleConnectModule = new TB_RoleConnectModule() { RoleID = intRoleID, ModuleID = item, }; _mTB_RoleConnectModuleDAL.Create(mTB_RoleConnectModule, _tran, _db); } //提交事务 _tran.Commit(); } catch { //回滚事务 _tran.Rollback(); } finally { //关闭连接 _connection.Close(); } return(intRoleID); }