前言
在上一篇文章中,小编为大家简单介绍了一下,三层的基础知识,三层:表现层UI,业务逻辑层BLL,数据访问层DAL。这一次我们主要针对实现窗体登录功能,来进一步介绍一下三层项目开发。
图解
首先,咱们一起看一下包图,就会对各层的调用关系有一个大致的了解。在这里小编纠正一个误区,UI层可以调用BLL层,调用Entity层,但是不可以反过来,也就是说BLL层不可以调用UI层,各层之间是单向的引用关系,而不是一些人认为的直线关联或是双向关系。另外,UI层可以B层,B层调用D层,那UI层可以直接调用DAL层吗?答案是不可以,否则,我们还费劲因解耦而分三层干什么啊,小编在最开始的时候就产生了这样一个误区,当然也吃了不少苦头。
图1 三层架构包图
实例演练
Entity层:
<strong><span style="font-family:KaiTi_GB2312;font-size:18px;"><strong style="background-color: rgb(255, 255, 255);">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LoginModel
{
public class UserInfo
{
public string UserName { get; set; }
public string Password { get; set; }
}
}
</strong></span></strong>
UI层:
<strong><span style="font-family:KaiTi_GB2312;font-size:18px;"><strong style="background-color: rgb(255, 255, 255);">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 UI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
string userName = txtUsername.Text.Trim();
string password = txtPassword.Text;
LoginBLL.LoginManager mgr = new LoginBLL.LoginManager();
LoginModel.UserInfo user = mgr.Login(userName, password);
MessageBox.Show("登录用户:" + user.UserName);
}
}
}</strong></span></strong>
<strong><span style="font-family:KaiTi_GB2312;font-size:18px;"><strong style="background-color: rgb(255, 255, 255);">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LoginBLL
{
public class LoginManager
{
public LoginModel.UserInfo Login(string userName, string password)
{
LoginDAL.UserDAO uDao = new LoginDAL.UserDAO();
LoginModel.UserInfo user=uDao.SelectUser(userName, password);
if (user!=null)
{
return user;
}
else
{
throw new Exception ("登录失败");
}
}
}
}</strong></span></strong>
DAL层:
<strong><span style="font-family:KaiTi_GB2312;font-size:18px;"><strong style="background-color: rgb(255, 255, 255);">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LoginDAL
{
class DbUtill
{
public static string ConnString = @"Server=(local);DataBase =Login; User ID =sa;Password =6";
}
}</strong></span></strong>
<strong><span style="font-family:KaiTi_GB2312;font-size:18px;"><strong style="background-color: rgb(255, 255, 255);">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace LoginDAL
{
public class UserDAO
{
public LoginModel.UserInfo SelectUser(string userName, string password)
{
using( SqlConnection conn = new SqlConnection(DbUtill.ConnString))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = @"SELECT * FROM Users WHERE UserName=@userName AND Password=@password";
cmd.CommandType =System.Data.CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@UserName",userName));
cmd.Parameters.Add(new SqlParameter("@Password",password));
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
LoginModel.UserInfo user = null;
while (reader.Read())
{
if (user==null)
{
user = new LoginModel.UserInfo();
}
user.UserName = reader.GetString(1);
user.Password = reader.GetString(2);
}
return user;
}
}
}
}
</strong></span></strong>
问题
图2 问题
一般的出现这个问题就是D层出现问题。检查一下代码是否正确,尤其是连接数据库的代码格外要仔细。下图中我提供了一种数据库的连接字符串。大家可以做一个参照。接着,问题依然存在,那咱们在看一下SQL语句有没有错误,而且多个空格,少个引号都是不可以的。
图3 数据库连接字符串
图4 数据库查询语句
总结
U层用于显示数据和接收用户输入的数据,为用户提供一种交互式操作的界面。B层主要是针对具体的问题的操作,也可以理解成对数据层的操作,对数据业务逻辑处理,如果说数据层是积木,那逻辑层就是对这些积木的搭建。D层是访问数据库,进行数据库的增删改查系列操作。 纸上得来终觉浅,绝知此事要躬行。