/// <summary> /// 删除数据 /// </summary> /// <param name="entity"></param> /// <returns></returns> public int Delete(UserRight entity) { string sql = "DELETE FROM tb_user_right WHERE userId=@userId and menuId =@menuId"; using (MySqlConnection mycn = new MySqlConnection(mysqlConnection)) { mycn.Open(); MySqlCommand command = new MySqlCommand(sql, mycn); command.Parameters.AddWithValue("@userId", entity.userId); command.Parameters.AddWithValue("@menuId", entity.menuId); int i = command.ExecuteNonQuery(); mycn.Close(); return i; } }
public const string mysqlConnection = DBConstant.mysqlConnection;//"User Id=root;Host=115.29.229.134;Database=chinaunion;password=c513324665;charset=utf8"; /// <summary> /// 添加数据 /// </summary> /// <returns></returns> public int Add(UserRight entity) { string sql = "INSERT INTO tb_user_right (userId, menuId) VALUE (@userId, @menuId)"; using (MySqlConnection mycn = new MySqlConnection(mysqlConnection)) { mycn.Open(); MySqlCommand command = new MySqlCommand(sql, mycn); command.Parameters.AddWithValue("@userId", entity.userId); command.Parameters.AddWithValue("@menuId", entity.menuId); int i = command.ExecuteNonQuery(); mycn.Close(); return i; } }
/// <summary> /// 修改数据 /// </summary> /// <param name="entity"></param> /// <returns></returns> public int Update(UserRight entity) { string sql = "UPDATE tb_user_right SET userId=@userId ,menuId=@menuId where userId=@userId "; //string sql = "UPDATE cimuser SET userNickName=@userNickName WHERE userid=@userid"; using (MySqlConnection mycn = new MySqlConnection(mysqlConnection)) { mycn.Open(); MySqlCommand command = new MySqlCommand(sql, mycn); command.Parameters.AddWithValue("@userId", entity.userId); command.Parameters.AddWithValue("@menuId", entity.menuId); int i = command.ExecuteNonQuery(); mycn.Close(); return i; } }
private void btnSave_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(this.txtNewUserId.Text.Trim())) { MessageBox.Show("请输入用户名!"); this.txtNewUserId.Focus(); return; } if (String.IsNullOrEmpty(this.txtName.Text.Trim())) { MessageBox.Show("请输入姓名!"); this.txtName.Focus(); return; } if (String.IsNullOrEmpty(this.txtPassword.Text.Trim())) { MessageBox.Show("请输入密码!"); this.txtPassword.Focus(); return; } this.Cursor = Cursors.WaitCursor; User user = new User(); user.userId = this.txtNewUserId.Text.Trim(); user.name = this.txtName.Text.Trim(); user.password = this.txtPassword.Text.Trim(); userRightDao.DeleteByUserId(user.userId); userDao.Delete(user.userId); userDao.Add(user); foreach (TreeNode node in this.tvMenu.Nodes) { if (node.Checked) { UserRight userRight = new UserRight(); userRight.userId = user.userId; userRight.menuId = node.Tag.ToString(); userRightDao.Add(userRight); } bool hasInsert = false; foreach (TreeNode childNode in node.Nodes) { if (childNode.Checked) { UserRight userRight = new UserRight(); userRight.userId = user.userId; userRight.menuId = childNode.Tag.ToString(); userRightDao.Add(userRight); if (!childNode.Parent.Checked && !hasInsert) { UserRight parentUserRight = new UserRight(); parentUserRight.userId = user.userId; parentUserRight.menuId = childNode.Parent.Tag.ToString(); userRightDao.Add(parentUserRight); hasInsert = true; } } } } /* foreach (DataGridViewRow row in this.dgAssignRight.Rows) { UserRight userRight = new UserRight(); userRight.userId = user.userId; userRight.menuId = row.Cells[0].Value.ToString(); userRightDao.Add(userRight); }*/ this.prepareGrid(""); MessageBox.Show("操作完成"); this.Cursor = Cursors.Default; }
/// <summary> /// 根据主键查询 /// </summary> /// <param name="primaryKey"></param> /// <returns></returns> public User Get(String userId) { string sql = "SELECT userId, name, password FROM tb_user where userId=@userId"; using (MySqlConnection mycn = new MySqlConnection(mysqlConnection)) { mycn.Open(); MySqlCommand command = new MySqlCommand(sql, mycn); command.Parameters.AddWithValue("@userId", userId); MySqlDataReader reader = command.ExecuteReader(); User user = null; UserRightDao userRightDao = new UserRightDao(); if (reader.Read()) { user = new User(); user.userId = reader["userId"] == DBNull.Value ? null : reader["userId"].ToString(); user.name = reader["name"] == DBNull.Value ? null : reader["name"].ToString(); user.password = reader["password"] == DBNull.Value ? null : reader["password"].ToString(); } reader.Close(); sql = "SELECT t1.userId,t1.menuId, t2.menu_text FROM tb_user_right t1, tb_menu t2 where userId=@userId and t1.menuId = t2.ID "; command = new MySqlCommand(sql, mycn); command.Parameters.AddWithValue("@userId", userId); MySqlDataReader userRightReader = command.ExecuteReader(); IList<UserRight> list = new List<UserRight>(); UserRight userRight = null; while (userRightReader.Read()) { userRight = new UserRight(); userRight.userId = userRightReader["userId"] == DBNull.Value ? null : userRightReader["userId"].ToString(); userRight.menuId = userRightReader["menuId"] == DBNull.Value ? null : userRightReader["menuId"].ToString(); userRight.menuText = userRightReader["menu_text"] == DBNull.Value ? null : userRightReader["menu_text"].ToString(); list.Add(userRight); } if (user != null) { user.userRightList = list; } mycn.Close(); return user; } }
/// <summary> /// 查询集合 /// </summary> /// <returns></returns> public IList<UserRight> GetList(String userId) { string sql = "SELECT t1.userId,t1.menuId, t2.menu_text FROM tb_user_right t1, tb_menu t2 where userId=@userId and t1.menuId = t2.ID "; using (MySqlConnection mycn = new MySqlConnection(mysqlConnection)) { mycn.Open(); MySqlCommand command = new MySqlCommand(sql, mycn); command.Parameters.AddWithValue("@userId", userId); MySqlDataReader reader = command.ExecuteReader(); IList<UserRight> list = new List<UserRight>(); UserRight userRight = null; while (reader.Read()) { userRight = new UserRight(); userRight.userId = reader["userId"] == DBNull.Value ? null : reader["userId"].ToString(); userRight.menuId = reader["menuId"] == DBNull.Value ? null : reader["menuId"].ToString(); userRight.menuText = reader["menu_text"] == DBNull.Value ? null : reader["menu_text"].ToString(); list.Add(userRight); } mycn.Close(); return list; } }