Three layer code annotation

U level

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

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //Obtain the account and password entered by the user and assign it to userName´╝îpassword
            string userName = txtUserName.Text.Trim();
            string password = txtPassword.Text;
            //instantiationBlayerLoginManager
            Login.BLL.LoginManager mgr = new Login.BLL.LoginManager();
            //mgrcall userlogin Method, encapsulate the obtained user name and password, and prepare to transmit data. Now turn toBlayer  
            Login.Model.UserInfo user = mgr.UserLogin(userName, password);

            MessageBox.Show("Login user:" + user.UserName);


        }
    }
}
  • Take out your nice little hand and set a breakpoint before btnLogin's Click event, then press F11 to start debugging step by step
  • In the above code, the account password data entered with your beautiful little hand is transmitted to layer B

B level

namespace Login.BLL
{
    //LoginManager class
    public class LoginManager
    {
        public Login.Model.UserInfo UserLogin(string userName, string password)
        {
            //throw new NotImplementedException();

            //Instantiate layer D
            Login.DAL.UserDAO uDao = new Login.DAL.UserDAO();
            //Jump to layer D and transfer the current userName and password to the entity layer
            Login.Model.UserInfo user = uDao.SelectUser(userName, password);
//-------------------------------------
            //If user is not null
            if (user != null)       //Landing successfully
            {
                //Instantiate level D ScoreDao
                Login.DAL.ScoreDAO sDao = new Login.DAL.ScoreDAO();

                //Update user points
                sDao.UpdateScore(userName, 10);     
                //Return user to layer U
                return user;
            }
            else
            {
                throw new Exception("Login failed");
            }
        }
    }
}
  • The upper part of the split line will jump to the UserDAO of layer D
  • Next, return user for layer D, and then jump to ScoreDAO of layer D to add user's integral

D layer.UserDAO

namespace Login.DAL
{
    public class UserDAO
    {
        //Two parameters are set [it's said to be a great idea], and the return type is Login.Model.UserInfo
        public Login.Model.UserInfo SelectUser(string userName, string password)
        {
            //Linked database, with using, the database will automatically close
            using(SqlConnection conn = new SqlConnection(DbUtil.ConnString))
            {
                //Establish a query and create a Command using an existing connection conn to execute SQL instructions
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = @"SELECT ID, UserName, Password,Email
                                    FROM USERS WHERE UserName=@UserName AND Password=@Password AND Password=@Password";
                //Assign SQL statement to cmd.commandtext
                cmd.CommandType = System.Data.CommandType.Text; 
                cmd.Parameters.Add(new SqlParameter("@UserName",userName));
                cmd.Parameters.Add(new SqlParameter("@Password",password));

                //Open query connection
                conn.Open();
                //To read the data one by one, use the ExecuteReader() method to return an object and use the reader to receive it  
                SqlDataReader reader = cmd.ExecuteReader();

                //Definition user is Null
                Login.Model.UserInfo user = null;
                //Loop, passing value to user
                while (reader.Read())
                {
                    if (user ==null)
                    {
                        user = new Login.Model.UserInfo();
                    }
                    //Assign to user when ID UserName Password is not empty
                    user.ID = reader.GetInt32(0);
                    user.UserName = reader.GetString(1);
                    user.Password = reader.GetString(2);
                    if (!reader.IsDBNull(3))
                    {
                        user.Email = reader.GetString(3);
                    }
                }
                //Return user to layer B
                return user;
            }
        }
    }
}
  • It's already here. If you forget, go back to it and have a look. "You said you didn't come down to look at floor B just now, oh, so? "

Level D.ScoreDAO

namespace Login.DAL
{
    public class ScoreDAO
    {
        //The UpdateScore method requires two parameters: userName and value
        public void UpdateScore(string userName, int value)
        {
            //Instantiate database connection conn
            using (SqlConnection conn = new SqlConnection(DbUtil.ConnString))
            {
                //Create a Command using the existing connection conn to execute SQL instructions
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = @"INSERT INTO SCORES(UserName,Score) Values (@UserName,@Score)";
                //Add new data to database, Score+10
                cmd.Parameters.Add(new SqlParameter("@UserName", userName));
                cmd.Parameters.Add(new SqlParameter("@Score",value));

                conn.Open();
                //Execute SQL instruction
                cmd.ExecuteNonQuery();

            }
        }
    }
}
  • After adding points, return to layer B and then to layer D, pop up the little box you want to see most: "login successfully"

Tags: Database SQL

Posted on Sun, 22 Mar 2020 08:56:49 -0700 by Zyxist