/// <summary> /// 绑定用户信息 /// </summary> private void BindUserInfo() { user = uBll.GetUser(_id); txtUserName.Text = user.UName; txtLoginName.Text = user.LoginName; txtPassword.Text = user.Password; txtConfirmPwd.Text = user.Password; }
private void btnOk_Click(object sender, EventArgs e) { string userName = txtUserName.Text.Trim(); string loginName = txtLoginName.Text.Trim(); string pwd = txtPassword.Text.Trim(); string confirmPwd = txtConfirmPwd.Text.Trim(); if (userName.Length == 0) { MessageBox.Show("姓名不能为空"); return; } if (loginName.Length == 0) { MessageBox.Show("用户名不能为空"); return; } if (pwd.Length == 0) { MessageBox.Show("密码不能为空"); return; } if (confirmPwd.Length == 0) { MessageBox.Show("确认密码不能为空"); return; } if (pwd != confirmPwd) { MessageBox.Show("两次输入的密码不一致"); return; } User user = new User(); user.UName = userName; user.LoginName = loginName; user.Password = pwd; uBll.Insert(user); MessageBox.Show("添加成功!"); this.DialogResult = DialogResult.OK; this.Close(); }
/// <summary> /// 获取所有用户列表 /// </summary> /// <returns>用户集合</returns> public IList<User> GetAllUser() { IList<User> list = new List<User>(); string sql = "select * from TB_User order by UName asc"; DataSet ds = AccessHelper.ExecuteDataSet(_connectionString, sql); if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { foreach (DataRow row in ds.Tables[0].Rows) { User user = new User(); user.UId = (int)row["UId"]; user.UName = (string)row["UName"]; user.LoginName = (string)row["LoginName"]; user.Password = (string)row["Password"]; list.Add(user); } } return list; }
/// <summary> /// 修改用户 /// </summary> /// <param name="user">用户对象</param> /// <returns>影响行数</returns> public int Update(User user) { return _dal.Update(user); }
/// <summary> /// 添加用户 /// </summary> /// <param name="user">用户对象</param> /// <returns>ID</returns> public int Insert(User user) { return _dal.Insert(user); }
/// <summary> /// 获取用户对象 /// </summary> /// <param name="eId">Id</param> /// <returns>用户对象</returns> public User GetUser(int id) { User user = null; string sql = "select * from TB_User where UId=" + id; DataSet ds = AccessHelper.ExecuteDataSet(_connectionString, sql); if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { DataRow row = ds.Tables[0].Rows[0]; user = new User(); user.UId = (int)row["UId"]; user.UName = (string)row["UName"]; user.LoginName = (string)row["LoginName"]; user.Password = (string)row["Password"]; } return user; }
/// <summary> /// 修改用户 /// </summary> /// <param name="user">用户对象</param> /// <returns>影响行数</returns> public int Update(User user) { string sql = "update TB_User set UName=@UName,LoginName=@LoginName,[Password]=@Password where UId=@UId"; OleDbParameter[] param = new OleDbParameter[] { new OleDbParameter("@UName",user.UName), new OleDbParameter("@LoginName",user.LoginName), new OleDbParameter("@Password",user.Password), new OleDbParameter("@UId",user.UId) }; int count = AccessHelper.ExecuteNonQuery(_connectionString, sql, param); return count; }
/// <summary> /// 添加用户 /// </summary> /// <param name="user">用户对象</param> /// <returns>ID</returns> public int Insert(User user) { string sql = "insert into TB_User(UName,LoginName,[Password]) "; sql += "values(@UName,@LoginName,@Password) "; OleDbParameter[] param = new OleDbParameter[] { new OleDbParameter("@UName",user.UName), new OleDbParameter("@LoginName",user.LoginName), new OleDbParameter("@Password",user.Password) }; AccessHelper.ExecuteNonQuery(_connectionString, sql, param); object id = AccessHelper.ExecuteScalar(_connectionString, "select @@identity"); return Convert.ToInt32(id); }
/// <summary> /// 根据用户名获取用户 /// </summary> /// <param name="loginName">用户名</param> /// <returns>用户对象</returns> public User GetUser(string loginName) { User user = null; string sql = "select * from TB_User where LoginName=@LoginName"; OleDbParameter[] param = new OleDbParameter[] { new OleDbParameter("@LoginName",loginName) }; DataSet ds = AccessHelper.ExecuteDataSet(_connectionString, sql, param); if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { DataRow row = ds.Tables[0].Rows[0]; user = new User(); user.UId = (int)row["UId"]; user.UName = (string)row["UName"]; user.LoginName = (string)row["LoginName"]; user.Password = (string)row["Password"]; } return user; }