三层之窗体登录

来源:互联网 发布:linux时间同步 chrony 编辑:程序博客网 时间:2024/05/16 14:04

前言:

  在第一遍看三层视频的时候,虽然把程序照着老师的步骤敲出来了,但是无论如何还是不太懂的。过年回来深感调代码无能为力,所以又把那个小视频看了3-4遍,终于懂那么一点点的眉目。知道昨天师傅的讲解,才醍醐灌顶。

内容:

  三层在上一篇博客已经详细的介绍过了请参照三层初学。这里就以登录窗体的Demo为例。详细的走一遍代码。

回顾:  

  代码之前,我们先还来回顾一下三层都有哪三层:

  表现层(UI):
  展现给用户的界面,即用户在使用一个系统的时候他的所见所得。依据应用规模的不同,所承受的负荷会有较大的差异,另外客户端的数目,应用的复杂程度都会对其造成一定的影响。
  业务逻辑层(BLL):对数据层的操作和业务的处理。接收用户的指令或者数据输入,提交给应用层做处理,同时负责将业务逻辑层的处理结果显示给用户。相比传统的应用方式,业务层对硬件的资源要求较低。
  数据层(DAL):直接操纵数据库,主要是增删改查的功能。存储数据的数据库服务器和处理数据和缓存数据的组件。组件将大量使用的数据放入系统的缓存库,以提高数据访问和处理的效率。

为什么:

  业务逻辑层是整个三层的核心内容,分层的原理就是在客户端和数据库之间加一个“中间层”,对数据的访问起到一个承上启下的作用。两者的访问都是通过中间层进行的,不能直接联系。

三者之间的依赖关系的体现: 

  数据访问层的类,直接访问数据库,实现基本记录操作。
  业务逻辑层的类,调用相关的数据访问类,实现用户所需功能。
  界面层:部署控件后,调用业务逻辑层的类,实现功能。

代码:

UI层:

<span style="font-family:Times New Roman;font-size:18px;color:#333333;">namespace LoginUI{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {                      string userName = txtUserName.Text.Trim();//把text文本框里的用户名赋值给userName            string password = txtPassword.Text;//密码赋值给password            Login.BLL.LoginMannager mgr = new Login.BLL.LoginMannager();//重新实例化bll层的类 赋值给mgr            Login.Model.UserInfo user = mgr.UserLogin(userName, password);//重新实例化实体层 user的登录名为userName,密码为password            if (user==null)            {                MessageBox.Show("denglu shibai");            }            else            {                Form2 f = new Form2();                f.Show();                MessageBox.Show("登录用户:" + user.UserName);//成功登录提示            }                    }</span>


BLL层:

<span style="font-family:Times New Roman;font-size:18px;color:#333333;">namespace Login.BLL{  public class LoginMannager    {        public Login.Model.UserInfo UserLogin(string userName,string Password)//用户登录        {                        Login.DAL.UserDAO uDao = new Login.DAL.UserDAO();//重新实例化D层的UserDao            Login.Model .UserInfo user = uDao.SelectUser(userName, Password);//创建一个user进入D层            if (user != null)//如果user不为空那么为第一次登录加十分            {                Login.DAL.SorceDAO sDao = new Login.DAL.SorceDAO();//重新实例化SD层                sDao.UpdateScore(userName, 10);//在数据库中更新                return user;//返回User            }            else//否则,登录失败            {                return null;                throw new Exception("登录失败");//抛出异常,登录失败            }            }    }}</span>

DAL层:

UserDAO:

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman';font-size:18px;">namespace Login.DAL</span>
<span style="font-family:Times New Roman;font-size:18px;color:#333333;">{   public  class UserDAO    {                                                                     public Login .Model.UserInfo SelectUser(string UserName,string Password)//设定两个参数username和Password        {            //有了using之后数据库会自动关闭            using (SqlConnection conn = new SqlConnection(DBUtil.ConnString))//重新实例化数据库连接            {                                SqlCommand cmd = conn.CreateCommand();//利用一个现有连接conn创建一个Command,用以执行sql指令;                cmd.CommandText = @"select ID,UserName,Password,Email from Users_Info where UserName=@userName and Password=@Password";                cmd.CommandType = CommandType.Text;//默认commandtype的为Text 所以这句可以删掉                cmd.Parameters.Add(new SqlParameter("@Username", UserName));//添加存储参数                cmd.Parameters.Add(new SqlParameter("@Password", Password));//添加存储参数                conn.Open();//打开数据库连接                SqlDataReader reader = cmd.ExecuteReader();//使用ExecuteReader()方法返回一个对象用reader接收                Login.Model.UserInfo user = null;//设置User的默认值为null                while (reader.Read())                {                    if (user == null )//如果user值为空则延迟加载                    {                        user = new Login.Model.UserInfo();                    }                    user.ID =reader.GetInt32(0);                    user.UserName  = reader.GetString(1);                    user.Password = reader.GetString(2);                    if(!reader .IsDBNull (3))//如果Email不是空的时候才去读                    {                        user.Email  = reader.GetString(3);                    }                                    }                return user; //返回User            }        }    }}</span>

ScoreDAO

<span style="font-family:Times New Roman;font-size:18px;color:#333333;">public  class SorceDAO    {        public void UpdateScore(string userName,int value)//有一个Uupdate方法需要传入userName和value两个参数        {            //有了using之后数据库会自动关闭            using (SqlConnection conn = new SqlConnection(DBUtil.ConnString))//重新实例化数据库连接为conn            {                SqlCommand cmd = conn.CreateCommand();//利用一个现有连接conn创建一个Command,用以执行sql指令;                cmd.CommandText = @"insert into Scores(UserName,Score) Values (@UserName,@Score)";//创建一个命令 在Scores表中插入U色那么和Scores                cmd.Parameters.Add(new SqlParameter("@UserName", userName));//添加存储参数                cmd.Parameters.Add(new SqlParameter("@Score", value));//添加存储参数                conn.Open ();//打开数据库                cmd.ExecuteNonQuery();//执行sql命令            }        }    }}</span>

DBUtil

<span style="font-family:Times New Roman;font-size:18px;color:#333333;">namespace Login.DAL{  class DBUtil    {        public static string ConnString = @"Server=HELLO\SQL2012; Database=Login;UID=sa;pwd=1";  //连接数据库    }}</span>

Entity层

<span style="font-family:Times New Roman;font-size:18px;color:#333333;">namespace Login.Model //在实体层设定用户ID和用户名密码还有Email等一系列的参数{    public class UserInfo    {        public int ID { get; set; }//用户ID设定为整型,其他为字符串型        public string UserName { get; set; }        public string Password{get;set;}        public string Email { get; set; }    }}</span>

总结:

三层的代码是完成了窗体的登录并且显示了主窗体。明天呢开始,向重构开战吧。

0 0