以下是学习笔记:
1,UI界面
2,用户对象类(Models中):
/// <summary> /// 用户实体类,要与数据库的表名一致 /// </summary> public class SysAdmin { public int LoginID { get; set; } public string LoginName { get; set; } public string LoginPwd { get; set; } public int Role { get; set; } }
3,用户对象的数据服务
using AutomaticStoreMotionModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AutomaticStoreMotionDal { /// <summary> /// 用户管理Service /// </summary> public class SysAdminService { /// <summary> /// 返回所有的用户集合(admin除外) /// </summary> /// <returns>用户对象集合</returns> public static List<SysAdmin> GetAllAdminDB() { return SqlSugarHelper.SqlSugarClient.Queryable<SysAdmin>().Where(c => c.LoginName.ToLower() != "admin").ToList(); } /// <summary> /// 验证登录用户结果(验证用户名和密码和数据库的是否一致) /// </summary> /// <param name="sysAdmin">用户对象</param> /// <returns>用户对象</returns> public static SysAdmin AdminLogin(SysAdmin sysAdmin) { var list= SqlSugarHelper.SqlSugarClient.Queryable<SysAdmin>().Where(c => c.LoginName.ToLower() ==sysAdmin.LoginName&&c.LoginPwd==sysAdmin.LoginPwd).ToList(); return list.Count == 0 ? null : list[0]; } /// <summary> /// 增:添加用户 /// </summary> /// <param name="admin">用户对象</param> /// <returns></returns> public static bool AddAdminDB(SysAdmin admin) { return SqlSugarHelper.SqlSugarClient.Insertable(admin).ExecuteCommand() == 1; } /// <summary> /// 根据用户名判断用户是否存在 /// </summary> /// <param name="loginName">用户名</param> /// <returns></returns> public static bool CheckLoginNameExit(string loginName) { return SqlSugarHelper.SqlSugarClient.Queryable<SysAdmin>().Where(c=>c.LoginName==loginName).Count()>0; } /// <summary> /// 改:根据用户名更新用户 /// </summary> /// <param name="admin">用户对象</param> /// <returns></returns> public static bool UpdateAdminDB(SysAdmin admin) { return SqlSugarHelper.SqlSugarClient.Updateable(admin).WhereColumns(c => c.LoginName).ExecuteCommand()==1; } /// <summary> /// 删:更加用户名删除用户 /// </summary> /// <param name="loginName">用户名</param> /// <returns></returns> public static bool DeleteAdminDB(string loginName) { return SqlSugarHelper.SqlSugarClient.Deleteable<SysAdmin>().Where(c => c.LoginName==loginName).ExecuteCommand() == 1; } /// <summary> /// 根据用户名称返回用户对象 /// </summary> /// <param name="loginName">用户名</param> /// <returns>用户对象</returns> public static SysAdmin GetSysAdminByLoginName(string loginName) { var query= SqlSugarHelper.SqlSugarClient.Queryable<SysAdmin>().Where(c => c.LoginName == loginName).ToList(); if (query.Count > 0) { return query[0]; } else { return null; } } } }
4,用户界面的增删改查
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using AutomaticStoreMotionDal; using AutomaticStoreMotionModels; namespace AutomaticStoreMotionPro { /// <summary> /// 枚举权限 /// </summary> public enum AdminRole { 作业员, 工程师, 主管 } public partial class FrmUserManager : Form { public FrmUserManager() { InitializeComponent(); this.dgv_data.AutoGenerateColumns = false; this.dgv_data.ReadOnly = true;//设置只读模式 UpdateAdmin(); } //更新 private void UpdateAdmin() { this.dgv_data.DataSource = null; this.dgv_data.DataSource = SysAdminService.GetAllAdminDB(); } //加行号 private void dgv_data_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { AutomaticStoreMotionDal.DataGridViewHelper.DgvRowPostPaint(this.dgv_data, e); } //格式转换 private void dgv_data_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.RowIndex >= 0) { if (e.ColumnIndex == 1 && e.Value != null)//密码 { //MD5解密密码 } if (e.ColumnIndex == 2 && e.Value != null)//权限 { //权限存在数据库是数字,把值转为枚举的字符串 e.Value = ((AdminRole) Convert.ToInt32(e.Value)).ToString(); } } } //双击事件:把当前对象填充到输入框,方便修改 private void dgv_data_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { string loginName = this.dgv_data.CurrentRow.Cells[0].Value.ToString(); SysAdmin admin = SysAdminService.GetSysAdminByLoginName(loginName); if (admin != null) { this.txt_loginName.Text = admin.LoginName; this.txt_loginPwd.Text = admin.LoginPwd; this.rdb_operation.Checked = admin.Role == 0; this.rdb_engineer.Checked = admin.Role == 1; this.rdb_manager.Checked = admin.Role == 2; } } } //增加用户 private void btn_add_Click(object sender, EventArgs e) { if (this.txt_loginName.Text.Length == 0) { MessageBox.Show("用户名称不得为空", "添加用户"); return; } if (this.txt_loginPwd.Text.Length == 0) { MessageBox.Show("用户密码不得为空", "添加用户"); return; } if (SysAdminService.CheckLoginNameExit(this.txt_loginName.Text)) { MessageBox.Show("该用户名称已经存在,请修改后再添加", "添加用户"); return; } SysAdmin admin=new SysAdmin() { LoginName = this.txt_loginName.Text.Trim(), LoginPwd = this.txt_loginPwd.Text.Trim(), Role = this.rdb_operation.Checked?0:this.rdb_engineer.Checked?1:2 }; if (SysAdminService.AddAdminDB(admin)) { MessageBox.Show("新用户:" + $"“{admin.LoginName}”" + "添加成功", "添加用户"); UpdateAdmin(); } else { MessageBox.Show("新用户:" + $"“{admin.LoginName}”" + "添加失败", "添加用户"); } } //修改用户 private void btn_modify_Click(object sender, EventArgs e) { if (this.txt_loginName.Text.Length == 0) { MessageBox.Show("用户名称不得为空", "修改用户"); return; } if (this.txt_loginPwd.Text.Length == 0) { MessageBox.Show("用户密码不得为空", "修改用户"); return; } //检测用户是否存在 if (!SysAdminService.CheckLoginNameExit(this.txt_loginName.Text)) { MessageBox.Show("该用户名称不存在,请添加后再修改", "修改用户"); return; } SysAdmin admin = new SysAdmin() { LoginName = this.txt_loginName.Text.Trim(), LoginPwd = this.txt_loginPwd.Text.Trim(), Role = this.rdb_operation.Checked ? 0 : this.rdb_engineer.Checked ? 1 : 2 }; if (SysAdminService.UpdateAdminDB(admin)) { MessageBox.Show("用户:" + $"“{admin.LoginName}”" + "信息修改成功", "修改用户"); UpdateAdmin(); } else { MessageBox.Show("用户:" + $"“{admin.LoginName}”" + "信息修改失败", "修改用户"); } } //删除用户 private void btn_delete_Click(object sender, EventArgs e) { if (this.dgv_data.SelectedRows.Count > 0) { string loginName = this.dgv_data.SelectedRows[0].Cells[0].Value.ToString(); if (SysAdminService.DeleteAdminDB(loginName)) { MessageBox.Show("用户:"+$"“{loginName}”"+"删除成功", "删除用户"); UpdateAdmin(); } else { MessageBox.Show("用户:" + $"“{loginName}”" + "删除失败", "删除用户"); } } else { MessageBox.Show("请选择要删除的用户", "删除用户"); } } //清空文本框 private void btn_clear_Click(object sender, EventArgs e) { this.txt_loginName.Clear(); this.txt_loginPwd.Clear(); this.rdb_operation.Checked = true; } } }