winform-通用的使用者管理介面--SqlSugar對資料庫的增刪改查
阿新 • • 發佈:2021-12-23
以下是學習筆記:
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; } } }