三层学习——爱在程序中的涌动

来源:互联网 发布:mac 风扇声音很大 编辑:程序博客网 时间:2024/05/22 13:50

【前言】

       爱就是我们的数据!爱的涌动就是数据的走向!学习三层的时候,代码不是很懂,如何分析一个程序就成了我的第一个难题!

       我的思路是:看看程序的入口在哪里?运行先走哪里,单步调试,看看每一步都走哪里,数据是如何伴随代码走的?这样就可以看着界面想象代码了!!

       通过这样的方式学习,我首先发现了,我们没有编辑过的代码。

【正文】

     程序入口:U层

       Program.cs

namespace LoginUI{    static class Program    {        /// <summary>        /// 应用程序的主入口点。        /// </summary>        [STAThread]        static void Main()//本程序从这里开始        {            Application.EnableVisualStyles();//此方法为应用程序启用可视样式就是让你的控件(包括窗体)显示出来。            Application.SetCompatibleTextRenderingDefault(false);//作用:在应用程序范围内设置控件显示文本的默认方式!            Application.Run(new frmLogin()); //转跳到form1.designer.cs        }    }}
       form1.designer.cs

namespace LoginUI{    partial class frmLogin    {        /// <summary>        /// 必需的设计器变量。        /// </summary>        private System.ComponentModel.IContainer components = null;        //转跳到form1,终于看到我编辑的U层窗体代码了。走到InitializeComponent();然后再回来,开始加载控件。
       form1.cs

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace LoginUI{    public partial class frmLogin : Form    {        public frmLogin()        {            InitializeComponent();            //这里是调用的方法,在Form1.Designer.cs定义的        } //当最后次走到这里时,窗体加载出来了。
                                         

        于是输入用户名和密码,点击!

        private void btnLogin_Click(object sender, EventArgs e)        {            string userName = txtUserName.Text.Trim();            //获取输入框的用户名   赋值给userName            string password = txtPassword.Text;            //获取输入框的密码     赋值给password            Login.BLL.LoginService mgr = new Login.BLL.LoginService();            //实例化B层的loginservice                       //mgr调用userlogin方法,将获取的用户名和密码封装,准备传数据。此时转跳到B层            Login.Model.UserInfo user = mgr.UserLogin(userName, password);//这里,去了B层            MessageBox.Show("登录用户:" + user.UserName);        }          //通过实例化BLL层LoginManger的一个对象叫mgr用生成的这个对象使用BLL层的方法处理用户名和密码

       业务逻辑:B层

          LoginService.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Login.BLL{     public class LoginService    {        public   Login.Model.UserInfo UserLogin(string userName, string password)        {            //throw new NotFiniteNumberException();            Login.DAL.UserDAO uDao = new Login.DAL.UserDAO();//实例化D层的UserDAO            Login.Model.UserInfo user = uDao.SelectUser(userName,password);
            //转跳到D层,把当前的userName,password传给实体层            //创建了一个user进入D层            if (user != null )//判断用户是否为空   如果不为空,登录一次加十分            {                Login.DAL.ScoreDAO  sDAO = new Login.DAL.ScoreDAO ();//实例化D层的scoreDAO                sDAO.UpdateScore (userName, 10);//跟新用户的积分                return user;//返回user   ????            }            else            {                throw new Exception("登录失败。");                     }        }    }}

             数据访问:D层

      UserDAO.cs   来自B层的Login.Model.UserInfo user = uDao.SelectUser(userName,password);

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data;using System.Data.SqlClient;namespace Login.DAL{    public class UserDAO    {        public  Login.Model.UserInfo SelectUser(string userName,string password)//设定两个参数        {                   using (SqlConnection conn = new SqlConnection(DbUtil.ConnString))//打开数据库链接                                                                             //有了using之后数据库会自动关闭             //重新实例化数据库连接             {                SqlCommand cmd = conn.CreateCommand();//建立一个查询  利用一个现有连接conn创建一个Command,用以执行sql指令;                cmd.CommandText = @"SELECT ID,UserName,Password FROM Users WHERE UserName=@UserName AND Password=@Password";                //将sql语句赋值给cmd.commandtext                cmd.CommandType = CommandType.Text;                            cmd.Parameters.Add(new SqlParameter("@UserName", userName));//将userName 的值赋给查询语句中user这个占位符                cmd.Parameters.Add(new SqlParameter ("@Password",password ));                conn.Open();//打开查询 连接                //Sqldatareader reader  = cmd.ExecuteReader();                SqlDataReader reader = cmd.ExecuteReader();//逐条的读取数据 使用ExecuteReader()方法返回一个对象用reader接收                Login.Model.UserInfo user = null;//设置User的默认值为null                  while (reader.Read())//读取数据,调用read方法,判断数据库是否有数据                {                              if (user == null)                                    {                                        user = new Login.Model.UserInfo();                                    }                                                   user.ID = reader.GetInt32(0);                               user.UserName = reader.GetString(1);                               user.Password = reader.GetString(2);                }            return user;//返回对象            }        }     }}

      ScoreDAO.cs   来自B层的   Login.DAL.ScoreDAO  sDAO = new Login.DAL.ScoreDAO ();

                                                    //实例化D层的scoreDAO
                                                    sDAO.UpdateScore (userName, 10);

                                                    //跟新用户的积分

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.SqlClient;namespace Login.DAL{     public class ScoreDAO    {         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命令            }        }    }}

【总结】

     总之,数据从U层获取,传到B层,接下来去D层执行操作,接着来到B层进行判断。然后将结果返回U层。

     小编的才疏学浅,请各位大牛多多指教!代码注释如果有不对的,也可以当面指出,谢谢!

原创粉丝点击