三层登录实现 C#版

来源:互联网 发布:httpclient工具类json 编辑:程序博客网 时间:2024/06/07 03:48

前言

在上边文章了解了什么是三层以及三层的作用等等后,就开始接触关于三层的代码了。第一次接触三层代码,第一遍是蒙圈状态,直到第二遍第三遍才渐渐了解三层的魅力。实现代码后按着断点走一遍,然后华伟给讲了讲,各层之间的跳跃,数据的查询、判断等,感谢华伟。

当然,实现代码的过程中出了不少错,文中介绍。


内容

代码段就按照程序走的顺序来吧,首先看UI层,U层代码如下:

        private void button1_Click(object sender, EventArgs e)        {            //读取用户输入的账号密码            string userName = txtUserName.Text.Trim();            string password = txtPassword.Text;            //实例化B层            Login.BLL.LoginManager mgr = new Login.BLL.LoginManager();            //进入B层并等待返回结果            Login.Model.UserInfo user = mgr.UserLogin(userName, password);            MessageBox.Show("登录成功:" + user.UserName);        }


如代码中的注释,这个时候应该把账号密码框中的数据传送到B层处理:

好,准备好了,嗖~


B层代码:

namespace Login.BLL{    //LoginManager类    public class LoginManager    {        public Login.Model.UserInfo UserLogin(string userName, string password)        {            //throw new NotImplementedException();            //实例化D层            Login.DAL.UserDAO uDao = new Login.DAL.UserDAO();            //跳转到D层,把结果传送回来            Login.Model.UserInfo user = uDao.SelectUser(userName, password);//---------------------------------------------------------------------------            //如果user不为空            if (user != null)//login successfully            {                //实例化D层ScoreDAO.cs                Login.DAL.ScoreDAO sDao = new Login.DAL.ScoreDAO();                //跳转到D层并执行相应操作                sDao.UpdateScore(userName, System.Data.CommandType.Text, 10);                //返回user到U层                return user;             }             else            {                throw new Exception("登陆失败。");            }        }    }}

如上代码,分割线前为跳转到D层UserDAO.cs代码

下面为D层返回值后的代码,带着D层返回的user

然后再跳转到D层的ScoreDAO,sc


D层代码(UserDAO.cs):

namespace Login.DAL{    public class UserDAO    {        //返回类型为Login.Model.UserInfo(目前还不太懂)        public Login.Model .UserInfo  SelectUser(string userName, string password)        {            //连接数据库            using (SqlConnection conn = new SqlConnection(DbUtil.ConnString))            {                SqlCommand cmd = conn.CreateCommand();                cmd.CommandText = @"SELECT ID,UserName,Password,Email FROM Users WHERE UserName =@UserName AND Password = @Password";                cmd.CommandType = CommandType.Text;                cmd.Parameters.Add(new SqlParameter("@UserName", userName));                cmd.Parameters.Add(new SqlParameter("@Password", password));                                //打开数据库                conn.Open();                                SqlDataReader reader = cmd.ExecuteReader();                //定义user为null                Login.Model.UserInfo user = null;                //循环,把值传给user                while (reader.Read())                {                    if (user == null)                    {                        user = new Login.Model.UserInfo();                    }                    //ID UserName Password不为空的情况下赋值给user                    user.ID = reader.GetInt32(0);                    user.UserName = reader.GetString(1);                    user.Password = reader.GetString(2);//not suggestion                    if (!reader.IsDBNull(3))                    {                        user.Email = reader.GetString(3);                    }                }                //返回User到B层                return user;            }          }    }}

这个时候就将值已经返回到B层了,如上边B层代码分割线一下,回到上面接着介绍。


D层ScoreADO.cs代码:

namespace Login.DAL{    public class ScoreDAO    {        public void UpdateScore(string userName,CommandType type, int value)        {            using (SqlConnection conn = new SqlConnection(DbUtil.ConnString))            {                SqlCommand cmd = conn.CreateCommand();                cmd.CommandType = type;                cmd.CommandText = @"INSERT INTO Scores(UserName,Score)Values(@UserName,@Score)";                //数据库中增加新的数据,Score+10                cmd.Parameters.Add(new SqlParameter("@UserName", userName));                cmd.Parameters.Add(new SqlParameter("@Score", value));                conn.Open();                cmd.ExecuteNonQuery();            }        }    }}

加积分后,返回B层然后再返回U层,提示‘登陆成功’。

程序结束。


总结

大致理解了三层之后还是很开心的,有一点让我特别感慨的是这么复杂的操作,计算机是在一瞬间完成了,点登陆后的同时给我登陆成功的反馈。

突然想到,前一段时间在‘得到’APP听罗振宇老师讲AI的时候,他说人不如机器最大的特点就是记忆力和运算速度。

未来人对机器的优势是什么呢?就是人会犯错,机器不会犯错。不管是人还是其他生物,都是在基因突变中优胜略汰进化而来,基因突变就是基因中犯的错,当这些错误对生存有利,就慢慢的保存了下来。

未来也是,在犯错中可能会有新的发现,就算没有也可以总结错误不是吗?

扯远了,哈哈。。。




0 0
原创粉丝点击