The previous article re-introduced the basic knowledge of the three-tier, today we will take a look at the specific applications of the three-tier
①, scene
There is an existing score system. Students can log in their user name and password to enter the system to query related score information
②, project materials
③, code display
Model layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Login.Model
{
public class UserInfo
{
private int id;
private string username;
private string password;
private string email;
//将标中的每个字段抽取为类的字段,并封装成属性
/// <summary>
/// 用户id
/// </summary>
public int ID
{
get { return id; }
set { id = value; }
}
/// <summary>
/// 用户名
/// </summary>
public string UserName
{
get { return username; }
set { username = value; }
}
/// <summary>
/// 密码
/// </summary>
public string Password
{
get { return password; }
set { password = value; }
}
/// <summary>
/// 邮件地址
/// </summary>
public string Email
{
get { return email; }
set { email = value; }
}
}
}
UI layers
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;
using BLL;
using Model;
namespace UI
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
}
#region 登录单击事件
private void btnLogin_Click(object sender, EventArgs e)
{
//UI 层将用户输入数据传递给BLL层
//第1步、定义两个变量用来接收用户输入的值
int userName = Convert.ToInt32(txtUserName.Text.Trim()); //变量,获取用户名
string password = txtPassword.Text; //变量,获取密码,有可能password是个空格就不用trim了
string msg = ""; //第2步、定义一个string类型变量,用来获取从BLL返回来的登陆提示信息
BLL.LoginManagerBLL lmBLL = new BLL.LoginManagerBLL(); //第3步、实例化B层服务类
Model.UserInfoEnity user = new Model.UserInfoEnity(); //第4步、实例化实体层对象user
msg = lmBLL.UserLogin(userName, password, out msg); //第5步、调用B层UserLogin方法,并把值传入B层,,将返回的提示符信息重新赋值给msg变量。
MessageBox.Show(msg); //第22步、BLL层返回业务层传递的数据给用户,弹出提示框
}
#endregion
}
}
BLL layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using Model;
namespace BLL
{
public class LoginManagerBLL
{
public string UserLogin(int userName, string password, out string msg)
{
UserDAL uDAL = new UserDAL(); //第6步、实例化D层对象
bool result = uDAL.CheckUserByIdAndName(userName, password); //第7步、调用D层方法,并传参。定义一个bool类型变量
//第19步、变量result,接收返回来的布尔类型值
if (result) //第20步、判断,如果查询到用户信息
{
msg = "登陆成功"; //弹出提示信息
}
else
{
msg = "用户名或密码不正确,登陆失败";
}
return msg; //第21步、UI层返回业务层传递的数据给用户
}
}
}
}
DAL layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Model;
namespace DAL
{
public class UserDAL
{
public bool CheckUserByIdAndName(int userName, string password)
{
//有了using,connection就可以自动关闭了
//实例化一个连接对象conn,并连接数据源DataSourceDAL
bool flag = false; //第8步、定义一个标签变量
using (SqlConnection conn = new SqlConnection(DataSourceDAL.Connstring)) //第9步、实例化连接对象conn并连接数据源
{
SqlCommand cmd = conn.CreateCommand(); //第10步、实例化执行对象cmd
cmd.CommandText = @"select * from Users where ID=@ID and Password =@Password"; //第11步、要执行的SQL语句
cmd.CommandType = CommandType.Text; //第12步、要执行的SQL语句是Text文本类型的
SqlParameter[] sqlparameters = new SqlParameter[] //第13步、定义一个参数数组,并给参数赋值
{
new SqlParameter("@ID",userName), //给参数赋值
new SqlParameter("@Password",password)
};
cmd.Parameters.AddRange(sqlparameters); //第14步、添加数组
conn.Open(); //第15步、打开连接
SqlDataReader reader = cmd.ExecuteReader(); //第16步、执行对象,
if (reader.Read()) //第17步、逐条读取数据,如果有数据
{
flag = true; //返回true,说明存在此管理员
}
return flag; //第18步、返回标签
}
}
}
}