【三层】C#版实战演练

前言

    在上一篇文章中,小编为大家简单介绍了一下,三层的基础知识,三层:表现层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>


BLL层:

<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层是访问数据库,进行数据库的增删改查系列操作。
    纸上得来终觉浅,绝知此事要躬行。




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 22
    评论
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨倩-Yvonne

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值