【三层】未经处理的异常

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/suzan_bingtong/article/details/82929995

问题描述

在这里插入图片描述
如果用户名密码不正确,出现错误,但是,此方法并未对错误进行处理。我们应该对出现的错误进行处理。

解决方案

B层

namespace Login.BLL
{
    public class LoginManager
    {
        public   Login.Model.UserInfo UserLogin(string userName, string password)
        {
            //通过字段去数据库调用该用户名与密码信息
            Login.DAL.UserDAO uDao = new Login.DAL.UserDAO();
            Login.Model.UserInfo user=uDao.SelectUser(userName,password);
            //判断输入的用户名密码是否存在,并返回结果提示给用户
            if (user != null)//如果登陆成功,则添加积分
            {
                Login.DAL.ScoreDAO sDao = new Login.DAL.ScoreDAO();
                sDao.UpdateScore(userName , 10);
                return user;
            }
            else
            {
                //登录失败,返回空
                return null ;
            }
        }
    }

U层

namespace LoginUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //读取用户名和密码
            string userName=txtUserName .Text.Trim ();
            string password = txtPassWord.Text;
            Login.BLL.LoginManager mgr = new Login.BLL.LoginManager();
            Login.Model.UserInfo user=mgr.UserLogin(userName, password );

            
            if (user!=null)
            {
                MessageBox.Show("登录用户:" + user.UserName);                
            }
            else
            {
                MessageBox.Show("登录失败!");
            }
        }
    }
}

结果

登录成功
在这里插入图片描述
登录失败
在这里插入图片描述

总结

到此三层Over!我们要敢于向权威发起挑战!

猜你喜欢

转载自blog.csdn.net/suzan_bingtong/article/details/82929995
今日推荐