管理员密码修改

文章目录

    • 1 管理员密码修改后台方法编写
    • 2 管理员修改密码UI实现

1 管理员密码修改后台方法编写

/// 
/// 修改管理员密码
/// 
/// 
/// 
public int ModifyPwd(SysAdmin objAdmin)
{
    string sql = "update Admins set LoginPwd='{0}' where LoginId={1}";
    sql = string.Format(sql, objAdmin.LoginPwd, objAdmin.LoginId);
    try
    {
        return SQLHelper.Update(sql);
    }
    catch (SqlException)
    {
        throw new Exception("应用程序和数据库连接出现问题!");
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

2 管理员修改密码UI实现

管理员密码修改_第1张图片
FrmMain.cs

//密码修改
public static FrmModifyPwd objFrmModifyPwd = null;
 private void tmiModifyPwd_Click(object sender, EventArgs e)
 {
     if (objFrmModifyPwd == null)
     {
         objFrmModifyPwd = new FrmModifyPwd();
         objFrmModifyPwd.Show();
     }
     else
     {
         objFrmModifyPwd.Activate();
         objFrmModifyPwd.WindowState = FormWindowState.Normal;
     }
 }

FrmModifyPwd.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Models;
using DAL;

namespace StudentManager
{
    public partial class FrmModifyPwd : Form
    {
        public FrmModifyPwd()
        {
            InitializeComponent();
        }
        //修改密码
        private void btnModify_Click(object sender, EventArgs e)
        {
            #region 密码验证
            if (this.txtOldPwd.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入原密码!", "提示信息");
                this.txtOldPwd.Focus();
                return;
            }
            if (this.txtOldPwd.Text.Trim() != Program.objCurrentAdmin.LoginPwd)
            {
                MessageBox.Show("请输入的原密码不正确!", "提示信息");
                this.txtOldPwd.Focus();
                this.txtOldPwd.SelectAll();
                return;
            }
            if (this.txtNewPwd.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入不少于6位的新密码!", "提示信息");
                this.txtNewPwd.Focus();
                return;
            }
            if (this.txtNewPwd.Text.Trim().Length < 6)
            {
                MessageBox.Show("新密码长度不能少于6位!", "提示信息");
                this.txtNewPwd.Focus();
                return;
            }
            if (this.txtNewPwdConfirm.Text.Trim().Length == 0)
            {
                MessageBox.Show("请再次输入新密码!", "提示信息");
                this.txtNewPwdConfirm.Focus();
                return;
            }
            if (this.txtNewPwdConfirm.Text.Trim() != this.txtNewPwd.Text.Trim())
            {
                MessageBox.Show("两次输入的新密码不一致!", "提示信息");
                return;
            }
            #endregion

            //修改密码
            try
            {
                SysAdmin objAdmin = new SysAdmin()
                {
                    LoginId = Program.objCurrentAdmin.LoginId,
                    LoginPwd = this.txtNewPwd.Text.Trim()
                };
                if (new SysAdminService().ModifyPwd(objAdmin) == 1)
                {
                    MessageBox.Show("密码修改成功,请妥善保管!", "成功提示");
                    //同时修改当前保存的用户密码
                    Program.objCurrentAdmin.LoginPwd = this.txtNewPwd.Text.Trim();
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
		
		private void FrmModifyPwd_FormClosed(object sender, FormClosedEventArgs e)
        {
            FrmMain.objFrmModifyPwd = null;
        }
    }
}

你可能感兴趣的:(C#)