/// <summary> /// 添加用户到某个角色(不删除原有角色) /// </summary> /// <param name="userId"></param> /// <param name="roleIds"></param> /// <returns></returns> public RespResult AddRolesToUser(int userId, List<int> roleIds) { RespResult result = new RespResult(); try { foreach (int roleId in roleIds) { SysRoleUser item = new SysRoleUser(); item.UserId = userId; item.RoleId = roleId; SysRoleUserAccessor.Instance.Insert(item); } result.Error = AppError.ERROR_SUCCESS; } catch (Exception e) { result.Error = AppError.ERROR_FAILED; result.ExMessage = e.ToString(); } return result; }
/// <summary> /// 添加数据 /// <param name="es">数据实体对象数组</param> /// <returns></returns> /// </summary> public bool Insert(SysRoleUser e) { MySqlConnection oc = ConnectManager.Create(); MySqlCommand _cmdInsertSysRoleUser = cmdInsertSysRoleUser.Clone() as MySqlCommand; bool returnValue = false; _cmdInsertSysRoleUser.Connection = oc; try { if (oc.State == ConnectionState.Closed) oc.Open(); //_cmdInsertSysRoleUser.Parameters["@SysRoleUserId"].Value = e.SysRoleUserId; _cmdInsertSysRoleUser.Parameters["@RoleId"].Value = e.RoleId; _cmdInsertSysRoleUser.Parameters["@UserId"].Value = e.UserId; _cmdInsertSysRoleUser.ExecuteNonQuery(); return returnValue; } finally { oc.Close(); oc.Dispose(); oc = null; _cmdInsertSysRoleUser.Dispose(); _cmdInsertSysRoleUser = null; } }
/// <summary> /// 修改指定的数据 /// <param name="e">修改后的数据实体对象</param> /// <para>数据对应的主键必须在实例中设置</para> /// </summary> public void Update(SysRoleUser e) { MySqlConnection oc = ConnectManager.Create(); MySqlCommand _cmdUpdateSysRoleUser = cmdUpdateSysRoleUser.Clone() as MySqlCommand; _cmdUpdateSysRoleUser.Connection = oc; try { if (oc.State == ConnectionState.Closed) oc.Open(); _cmdUpdateSysRoleUser.Parameters["@SysRoleUserId"].Value = e.SysRoleUserId; _cmdUpdateSysRoleUser.Parameters["@RoleId"].Value = e.RoleId; _cmdUpdateSysRoleUser.Parameters["@UserId"].Value = e.UserId; _cmdUpdateSysRoleUser.ExecuteNonQuery(); } finally { oc.Close(); oc.Dispose(); oc = null; _cmdUpdateSysRoleUser.Dispose(); _cmdUpdateSysRoleUser = null; GC.Collect(); } }
/// <summary> /// 获取指定记录 /// <param name="id">Id值</param> /// </summary> public SysRoleUser Get(int SysRoleUserId) { SysRoleUser returnValue = null; MySqlConnection oc = ConnectManager.Create(); MySqlCommand _cmdGetSysRoleUser = cmdGetSysRoleUser.Clone() as MySqlCommand; _cmdGetSysRoleUser.Connection = oc; try { _cmdGetSysRoleUser.Parameters["@SysRoleUserId"].Value = SysRoleUserId; if (oc.State == ConnectionState.Closed) oc.Open(); MySqlDataReader reader = _cmdGetSysRoleUser.ExecuteReader(); if (reader.HasRows) { reader.Read(); returnValue = new SysRoleUser().BuildSampleEntity(reader); } } finally { oc.Close(); oc.Dispose(); oc = null; _cmdGetSysRoleUser.Dispose(); _cmdGetSysRoleUser = null; GC.Collect(); } return returnValue; }