用户登录与实现

版权声明:我的博客都是抄的,抄了太多就懒得注明出处了,请原谅! https://blog.csdn.net/SlowIsFastLemon/article/details/88838693

1 登录窗体的显示

1.1 添加登录窗体和对应的实体类

在这里插入图片描述

1.2 用户登录逻辑分析

在这里插入图片描述

1.3 代码编写

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;


namespace StudentManager
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //创建登录窗体
            FrmUserLogin objFrmLogin = new FrmUserLogin();
            DialogResult result = objFrmLogin.ShowDialog();


            //判断登录是否成功
            if (result == DialogResult.OK)
                Application.Run(new FrmMain());
            else
                Application.Exit();//退出整个应用程序

			//定义一个全局变量保存用户对象
        	public static Models.SysAdmin objCurrentAdmin = null;
        }
    }
}


2 登录后台方法编写

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using Models;

namespace DAL
{
    /// <summary>
    /// 管理员数据访问类
    /// </summary>
    public class SysAdminService
    {
        /// <summary>
        /// 根据登录账号和密码登录
        /// </summary>
        /// <param name="objAdmin">封装了登录账号和密码的管理员对象</param>
        /// <returns>返回包含管理员信息的对象</returns>
        public SysAdmin AdminLogin(SysAdmin objAdmin)
        {
            //组合SQL语句
            string sql = "select AdminName from Admins where LoginId={0}  and LoginPwd='{1}'";
            sql = string.Format(sql, objAdmin.LoginId, objAdmin.LoginPwd);
            //从数据库中查询
            SqlDataReader objReader = SQLHelper.GetReader(sql);
            if (objReader.Read())
            {
                objAdmin.AdminName = objReader["AdminName"].ToString();
            }
            else
            {
                objAdmin = null;//如果登录不成功,则将当前对象清空
            }
            objReader.Close();
            //返回结果
            return objAdmin;
        }
    }
}

3 登录窗体中的事件处理

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 DAL;
using Models;


namespace StudentManager
{
    public partial class FrmUserLogin : Form
    {
        //创建数据访问类对象
        private SysAdminService objAdminService = new SysAdminService();

        public FrmUserLogin()
        {
            InitializeComponent();
        }


        //登录
        private void btnLogin_Click(object sender, EventArgs e)
        {
            //【1】数据验证
            if (this.txtLoginId.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入登录账号!", "登录提示");
                this.txtLoginId.Focus();
                return;
            }
            if (this.txtLoginPwd.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入登录密码!", "登录提示");
                this.txtLoginPwd.Focus();
                return;
            }
            //【2】封装对象(实际封装的是用户登录账号和密码)
            SysAdmin objAdmin = new SysAdmin()
            {
                LoginId = Convert.ToInt32(this.txtLoginId.Text.Trim()),
                LoginPwd = this.txtLoginPwd.Text.Trim()
            };
            //【3】和后台交互,判断登录信息是否正确
            try
            {
                objAdmin = objAdminService.AdminLogin(objAdmin);
                if (objAdmin != null)//如果登录成功
                {
                    //保存登录信息
                    Program.objCurrentAdmin = objAdmin;
                    //设置登录窗体的返回值
                    this.DialogResult = DialogResult.OK;
                    //关闭窗体
                    this.Close();
                }
                else
                {
                    MessageBox.Show("登录账号或密码有误!", "登录提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据访问出现异常,登录失败!具体原因:" + ex.Message);
            }

        }
        //关闭
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      
        #region 改善用户体验

        private void txtLoginId_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)	// 回车键
            {
                if (this.txtLoginId.Text.Trim().Length != 0)
                {
                    this.txtLoginPwd.Focus();//将当前的焦点跳转到密码框
                }                
            }
        }
        private void txtLoginPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13) // 回车键
            {
                btnLogin_Click(null, null);//直接调用登录按钮的事件
            }
        }

        #endregion

      
    }
}

猜你喜欢

转载自blog.csdn.net/SlowIsFastLemon/article/details/88838693