winform-通用的用戶管理界面--SqlSugar對數據庫的增刪改查


以下是學習筆記:

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;
        }
    }
}

  

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM