private void btnAddUser_Click(object sender, EventArgs e) { Exception exError = null; try { #region 进行界面控制 if (txtComment.Text.Length > 200) { ErrorHandle.ShowInform("提示", "备注内容太长!"); txtComment.Focus(); return; } if (txtTrueName.Text.IndexOf(" ") != -1) { errorProvider.SetError(txtTrueName, "用户名不能有空格"); return; } //验证 if (string.IsNullOrEmpty(this.txtTrueName.Text)) { errorProvider.SetError(txtTrueName, "用户真实名不能为空!"); return; } else if (string.IsNullOrEmpty(this.txtPassword.Text)) { errorProvider.SetError(txtPassword, "密码不能为空!"); return; } else if (this.comboSex.Text == "") { errorProvider.SetError(comboSex, "请选择用户角色!"); return; } else if (this.comboSex.Text == "请选择") { errorProvider.SetError(comboSex, "请选择性别!"); return; } else if (string.IsNullOrEmpty(this.txtPosition.Text)) { errorProvider.SetError(txtPosition, "姓名不能为空!"); return; } if (txtTrueName.Text == "Admin") { ErrorHandle.ShowInform("提示", "此用户已存在,请重新输入!"); txtTrueName.Focus(); return; } #endregion //l连接系统维护库 SysCommon.DataBase.SysTable pSysDB = null; ModDBOperate.ConnectDB(out pSysDB, out exError); if (exError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", exError.Message); return; } #region 首先要判断用户名不能重复 //用户名不能与超级管理员同名 string[] arr = ModuleData.v_AppConnStr.Split(';'); if (arr.Length != 3) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "连接字符串不正确!"); return; } if (txtTrueName.Text.Trim() == arr[1].Substring(arr[1].IndexOf('=') + 1)) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "存在同名的用户!"); pSysDB.CloseDbConnection(); return; } //不能与数据库中的用户同名 string str = "select * from userbaseinfo where USERNAME='******'"; DataTable pUserTable = pSysDB.GetSQLTable(str, out exError); if (exError != null || pUserTable == null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "查询用户基本信息表失败!"); pSysDB.CloseDbConnection(); return; } #endregion //判断是更新还是添加 if (_user != null) { // (修改用户) 判断是否存在同名用户 if (pUserTable.Rows.Count > 1) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "存在同名的用户!"); pSysDB.CloseDbConnection(); return; } #region 用户修改,1、需要修改用户基本信息表,2、需要修改用户角色关系表,3、更新用户登录信息 pSysDB.StartTransaction(); //更新用户信息 #region 更新用户信息表 //若用户的基本信息发生了变更,则更新用户基本信息表 int pUserID = _user.ID; //用户ID int pRoleTypeID = _user.RoleTypeID; string updateStr = ""; if (m_UserName != txtTrueName.Text.Trim() || m_Password != txtPassword.Text.Trim() || comboSex.Text.Trim() != m_Sex || txtPosition.Text.Trim() != m_Position || txtComment.Text.Trim() != m_Remark) { //只要其中有一项基本信息发生了变化,就更injiben信息表 updateStr = "update userbaseinfo set UserName='******', Password='******', Sex='" + comboSex.Text.Trim() + "', Job='" + txtPosition.Text.Trim() + "', Remark='" + txtComment.Text.Trim() + "' where UserID=" + pUserID; pSysDB.UpdateTable(updateStr, out exError); if (exError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "更新用户信息表失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } } #endregion if (cmbRole.Text.Trim() != m_OldRoleName) { //说明用户对应的角色也进行了更新 #region 更新用户角色关系表 if ((cmbRole.SelectedItem as ComboBoxItem).Value == null || (cmbRole.SelectedItem as ComboBoxItem).Value.ToString().Trim() == "") { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "获取角色ID失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } int pRoleID = -1; //角色ID Convert.ToInt32((cmbRole.SelectedItem as ComboBoxItem).Value.ToString().Trim()); string mStr = "select * from rolebaseinfo where ROLENAME='" + cmbRole.Text.Trim() + "'"; DataTable tTable = pSysDB.GetSQLTable(mStr, out exError); if (exError != null || tTable == null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "查询角色信息表失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } if (tTable.Rows.Count == 0) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "获取角色ID失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } pRoleID = Convert.ToInt32(tTable.Rows[0][0].ToString().Trim()); //首先查询用户角色关系表,看是否存在这个记录 //****************************************************************** //guozheng added 2011-3-24 string sSQL = "SELECT ROLEID FROM rolebaseinfo WHERE ROLETYPEID=" + _user.RoleTypeID; DataTable GetTable = pSysDB.GetSQLTable(sSQL, out exError); if (exError != null || GetTable == null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "查询用户角色关系表失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } if (GetTable.Rows.Count < 0) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "没有找到该用户对应的角色信息"); return; } //****************************************************************** string tempStr = "select * from userrolerelationinfo where ROLEID=" + GetTable.Rows[0][0].ToString() + " and USERID=" + pUserID; DataTable temTable = pSysDB.GetSQLTable(tempStr, out exError); if (exError != null || temTable == null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "查询用户角色关系表失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } if (temTable.Rows.Count == 0) { //若不存在,则插入关系记录 updateStr = "insert into userrolerelationinfo(USERID,ROLEID) values (" + pUserID + "," + pRoleID + ")"; } else { //若存在,则更新关系记录 updateStr = "update userrolerelationinfo set ROLEID=" + pRoleID + " where USERID=" + pUserID; } pSysDB.UpdateTable(updateStr, out exError); if (exError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "更新用户角色关系表失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } #endregion //获得角色类型 tempStr = "select * from rolebaseinfo where ROLEID=" + pRoleID; temTable = pSysDB.GetSQLTable(tempStr, out exError); if (exError != null || temTable == null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "查询角色基本信息表失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } if (temTable.Rows.Count == 0) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "不存在该角色"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } pRoleTypeID = Convert.ToInt32(temTable.Rows[0][2].ToString().Trim()); } pSysDB.EndTransaction(true); pSysDB.CloseDbConnection(); #region 更新用户登录信息 ModuleData.m_User = new User(); ModuleData.m_User.ID = pUserID; ModuleData.m_User.Name = txtTrueName.Text.Trim(); ModuleData.m_User.Password = txtPassword.Text.Trim(); ModuleData.m_User.Sex = comboSex.Text.Trim(); ModuleData.m_User.Position = txtPosition.Text.Trim(); ModuleData.m_User.Remark = txtComment.Text.Trim(); ModuleData.m_User.Role = cmbRole.Text.Trim(); ModuleData.m_User.RoleTypeID = pRoleTypeID; #endregion SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "修改用户信息成功!"); #endregion } else { //(新增用户)判断是否存在同名用户 if (pUserTable.Rows.Count > 0) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "存在同名的用户!"); pSysDB.CloseDbConnection(); return; } //添加用户信息 #region 添加用户,1、需要添加用户基本信息表,2、需要添加用户角色关系表 pSysDB.StartTransaction(); #region 获得用户ID int pUserID = -1; string seleStr = "select Max(UserID) from userbaseinfo"; DataTable tempTable = pSysDB.GetSQLTable(seleStr, out exError); if (exError != null || tempTable == null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "查询用户信息表失败!"); pSysDB.CloseDbConnection(); return; } if (tempTable.Rows.Count == 0) { pUserID = 1; } else { if (tempTable.Rows[0][0].ToString().Trim() == "") { pUserID = 1; } else { pUserID = Convert.ToInt32(tempTable.Rows[0][0].ToString().Trim()) + 1; } } if (pUserID == -1) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "获取用户ID失败!"); return; } #endregion #region 更新用户基本信息表 string insertStr = "insert into userbaseinfo(UserID,UserName,Password,Sex,Job,Remark) values(" + pUserID + ",'" + txtTrueName.Text.Trim() + "','" + txtPassword.Text.Trim() + "','" + comboSex.Text.Trim() + "','" + txtPosition.Text.Trim() + "','" + txtComment.Text.Trim() + "')"; pSysDB.UpdateTable(insertStr, out exError); if (exError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "更新用户表信息失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } #endregion #region 更新用户角色表 //获得角色ID int pRoleID = -1; //角色ID if ((cmbRole.SelectedItem as ComboBoxItem).Value == null || (cmbRole.SelectedItem as ComboBoxItem).Value.ToString().Trim() == "") { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "获取角色ID失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } pRoleID = Convert.ToInt32((cmbRole.SelectedItem as ComboBoxItem).Value.ToString().Trim()); //插入数据到角色关系表里面 string inseStr = "insert into userrolerelationinfo(UserID,RoleID) values(" + pUserID + "," + pRoleID + ")"; pSysDB.UpdateTable(inseStr, out exError); if (exError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "更新用户角色关系表失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } #endregion pSysDB.EndTransaction(true); pSysDB.CloseDbConnection(); SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示 ", "添加用户信息成功!"); #endregion } this.DialogResult = DialogResult.OK; } catch (Exception ex) { exError = ex; } }
private void btnDelSel_Click(object sender, EventArgs e) { if (LstViewRole.CheckedItems.Count == 0) { return; } if (m_BeRole) { #region 除角色及其相关信息,1、删除角色用户关系表,2、删除角色表 if (SysCommon.Error.ErrorHandle.ShowFrmInformation("是", "否", "是否删除选中角色及其对应的所有用户?")) { //删除 SysCommon.DataBase.SysTable pSysDB = null; Exception outError = null; //连接数据库 ModDBOperate.ConnectDB(out pSysDB, out outError); if (outError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", outError.Message); return; } //开启事物 pSysDB.StartTransaction(); //遍历列表框,执行删除操作 for (int i = 0; i < LstViewRole.Items.Count; i++) { int pRoleID = -1;//角色ID //***********************************// //guozheng added if (pRoleID == 3) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "系统管理员角色:" + LstViewRole.Items[i].Text + " 不能删除"); continue; } ListViewItem pItem = new ListViewItem(); pItem = LstViewRole.Items[i]; if (!pItem.Checked) { continue; } pRoleID = Convert.ToInt32(pItem.Tag.ToString()); string delStr = ""; //删除字符串 //删除角色用户关系表 delStr = "delete from userrolerelationinfo where ROLEID=" + pRoleID; pSysDB.UpdateTable(delStr, out outError); if (outError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "删除用户角色关系表失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } //删除角色信息表 delStr = "delete from rolebaseinfo where ROLEID=" + pRoleID; pSysDB.UpdateTable(delStr, out outError); if (outError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "删除角色信息表失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } } pSysDB.EndTransaction(true); pSysDB.CloseDbConnection(); SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "删除成功!"); } #endregion } else { #region 除用户及其相关信息,1、删除角色用户关系表,2、删除用户基本信息表 if (SysCommon.Error.ErrorHandle.ShowFrmInformation("是", "否", "是否删除选中用户?")) { //删除 SysCommon.DataBase.SysTable pSysDB = null; Exception outError = null; //连接数据库 ModDBOperate.ConnectDB(out pSysDB, out outError); if (outError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", outError.Message); return; } //开启事物 pSysDB.StartTransaction(); //遍历列表框,执行删除操作 for (int i = 0; i < LstViewRole.Items.Count; i++) { int pUserID = -1;//角色ID ListViewItem pItem = new ListViewItem(); pItem = LstViewRole.Items[i]; if (!pItem.Checked) { continue; } pUserID = Convert.ToInt32(pItem.Tag.ToString()); string delStr = ""; //删除字符串 //删除角色用户关系表 delStr = "delete from userrolerelationinfo where USERID=" + pUserID; pSysDB.UpdateTable(delStr, out outError); if (outError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "删除用户角色关系表失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } //删除该用户的作业分配区域 delStr = "delete from UPDATEINFO where USERID=" + pUserID; pSysDB.UpdateTable(delStr, out outError); if (outError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "删除用户角色关系表失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } //删除角色信息表 delStr = "delete from userbaseinfo where USERID=" + pUserID; pSysDB.UpdateTable(delStr, out outError); if (outError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "删除角色信息表失败!"); pSysDB.EndTransaction(false); pSysDB.CloseDbConnection(); return; } } pSysDB.EndTransaction(true); pSysDB.CloseDbConnection(); SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "删除成功!"); } #endregion } this.Close(); }