前言

前期了解三层架构主要是由UI层、BLL层和DAL层三部分构成。看到大牛们都采用三层的思想实现了登录,本菜鸟暗暗地站在了他们的肩膀上。

自己理解

对于三层自己的理解是:就像我们对一个大型的公司去找人门口一定会有门卫,假设你就是一个用户,要去公司找该总经理洽谈业务。这时你会来到门口(UI层),要进去找人,门卫(BLL层)说:“你是谁?你找谁?干什么?等等各种问题。”然后接着就去查询档案(DAL层)。确定总经理是否认识这么个人。就这样“UI层——>BLL层——>DAL层”,然后DAL层给予回应见还是不见;传输层为:“DAL层——>BLL层——>UI层”。

这样门卫因为自己根本不认识这个人,所以不能因为不让他见总经理而得罪他,起到了一个“桥”的作用。在软件开发中,这样有利于减少代码之间的耦合度。从而方便了后期的维护,大大减轻了软件危机的到来。很棒吧~~~

三层架构优缺点

优点

1、符合开闭原则,便于用新的实现去替换原有的实现;

2、降低层与层之间的耦合度;

3、便于后期的复用;

4、标准化,看着舒服;

不足点

1、降低了系统的性能;

2、有时候会导致级联修改;还拿上面的例子来说:若你想再向总经理传达一件事情,可能还需要通过门卫也就是说UI层增加一些功能,BLL层和DAL层可能为了附和这个功能去添加相应的代码;

以上得出;在用三层架构的时候,合适的也是最好的,不要滥用。

C#版三层登录

  
             

Entity实体层

<span style="font-family:KaiTi_GB2312;font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Login.Model
{
public class UserInfo
{
public int ID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Email { get; set; }
}
}</span>

D层

<span style="font-family:KaiTi_GB2312;font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Login.DAL
{
public class DbUtil
//server是服务器的名称可以使Local,IP地址,计算机名称;
//DataBase是数据库的名称;
//User ID是登录服务器的用户名;
//Password是登录服务器的密码;
{
public static string ConnString = @"Server=ZLT;Database=Login;User ID=sa;Password=123"; ////下面是用windows身份验证模式链接数据库的;
//public static string ConnString = @"server=.;Database=Login;Integrated Security=ture"; }
}</span>
<span style="font-family:KaiTi_GB2312;font-size:18px;">using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data; namespace Login.DAL
{
public class UserDAO
{
public Login.Model.UserInfo SelectUser(string userName,string password)
{
using (SqlConnection conn = new SqlConnection(DbUtil.ConnString)) //数据库的链接;引用链接数据库的字符串
{
SqlCommand cmd = conn.CreateCommand();//定义命令语句 cmd.CommandText = @"SELECT ID,UserName,Email
FROM USERS WHERE UserName=@UserName AND Password=@Password";//SQL语句,查询表USERS
//cmd.CommandType = Commandtype.Text;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@UserName", userName)); //为参数USERNAME,PASSWORD赋值;
cmd.Parameters.Add(new SqlParameter("@Password", password)); conn.Open();//打开数据库; SqlDataReader reader = cmd.ExecuteReader(); //读取数据; Login.Model.UserInfo user = null;
while (reader.Read())
{
if (user == null)
{
user = new Login.Model.UserInfo();
} user.ID = reader.GetInt32(0);//给实体层属性写入数据;
user.UserName = reader.GetString(1);
user.Password = reader.GetString(2);
if (!reader .IsDBNull (2))
{
user.Email = reader.GetString(2);
}
} return user;//将实体返回B层; }
}
}
}</span>

B层

<span style="font-family:KaiTi_GB2312;font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Login.BLL
{
public class LoginManager
{
public Login.Model.UserInfo UserLogin(string userName, string password)
{
Login.DAL.UserDAO uDao = new Login.DAL.UserDAO(); //引用D层的UserDAO类
Login.Model.UserInfo user = uDao.SelectUser(userName, password); //引用实体层的SelectUser方法 if (user != null)
{
return user; //将实体user返回到U层
}
else
{
throw new Exception("登录失败。");//给系统错误进行提示; }
}
}
}
</span>

U层


<span style="font-family:KaiTi_GB2312;font-size:18px;">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 Login.UI; namespace LoginUI
{
public partial class 系统登录 : Form
{
public 系统登录()
{
InitializeComponent();
} private void btnLogin_Click(object sender, EventArgs e)
{
//LoginBLL. svr = new LoginBLL.LoginService();
string userName = txtUserName.Text.Trim();//向实体层的属性写入数据
string password = txtPassword.Text;
Login.BLL.LoginManager manage = new Login.BLL.LoginManager();//引用实体层
Login.Model.UserInfo user = manage.UserLogin(userName, password);//将UI层的用户名和密码通过B层传到实体层; MessageBox.Show("登录用户:" + userName); }
}
}</span>

小结

    1、学会站在巨人的肩膀上,自己也不失自己的思想;
    2、多多动手去实践,原来自己没有想象中那么笨。



感谢您的宝贵时间~~~

三层登录——C#版的更多相关文章

  1. 三层登录——VB.NET版

    前言 由于下面的机房收费系统重构自己要用VB.NET进行重构,所以在敲三层登录的时候,实践了一份C#版三层登录,接着就是VB.NET版的三层登录.话说还有七层登录,一下子感觉三层又矮小了.万丈高楼平地 ...

  2. 三层登录实例VB.NET版详解---理论加实战篇

    层,百度百科这样解释,首先-重叠起来的东西:重叠起来的东西中的一部分:层次|表层|大气层.其次-重叠:重复:层峦叠嶂|层出不穷.最后-量词,用于可以分出层次的事物,女孩儿强烈的第六感,三层中的层一定是 ...

  3. 三层登录实例VB.NET版具体解释---理论加实战篇

    层,百度百科这样解释,首先-重叠起来的东西:重叠起来的东西中的一部分:层次|表层|大气层.其次-重叠.反复:层峦叠嶂|层出不穷.最后-量词,用于能够分出层次的事物.女孩儿强烈的第六感,三层中的层一定是 ...

  4. 三层登录—c#

    学习了三层,有一个登录窗口的小练习.是我们第一次接触三层的初战.如今仅仅是简单的了解了一些,须要学习的还有非常多,以下浅谈自己的理解. 我们说的三层就是分层了显示层.业务逻辑层和数据訪问层.当中显示层 ...

  5. 三层——vb.net版

    经过不懈的努力,我的vb.net 版的三层登陆终于实现了.下面将我的成果向大家展示一下. 原则          vb.net的三层登陆跟C#的三层登陆的思想是一样的都是将系统分层--U层只负责与用户 ...

  6. 简单的模拟登录Wap版新浪微博

    环境:Ubuntu 16.04 python版本3.5+ import requests, lxml from bs4 import BeautifulSoup from io import Byte ...

  7. Asp.Net微信登录-电脑版扫描二维码登录

    像京东,一号店等网站都实现了用微信来登录的功能,就是用手机上的微信扫一扫网站上的二维码,微信上确认后,即可自动用微信的帐号登录网站. 一.创建网站应用 在微信开放平台创建一个网站应用 https:// ...

  8. C#微信登录-电脑版扫描二维码登录

    像京东,一号店等网站都实现了用微信来登录的功能,就是用手机上的微信扫一扫网站上的二维码,微信上确认后,即可自动用微信的帐号登录网站. 一.创建网站应用 在微信开放平台创建一个网站应用 https:// ...

  9. 如何从零开始对接第三方登录(Java版):QQ登录和微博登录

    前言 个人网站最近增加了评论功能,为了方便用户不用注册就可以评论,对接了QQ和微博这2大常用软件的一键登录,总的来说其实都挺简单的,可能会有一点小坑,但不算多,完整记录下来方便后来人快速对接. 后台设 ...

随机推荐

  1. IBatis笔记

    dynamic可以去除第一个prepend="and"中的字符(这里为and),从而可以帮助你实现一些很实用的功能 ibatis的remapResults属性在查询列发生变化,直接 ...

  2. HTML5 学习记录——2

    20150826 1.声明文档类型 <!DOCTYPE>  声明HTML是用什么版本写的. 常用声明; 2.HYML头部元素   <head> <title> 定义 ...

  3. php 冒泡排序原理

    $start = microtime(true);   $popArr = array(6,3,23,1,5,100,399,99,66);   echo '6,3,23,1,5,100,399,99 ...

  4. 【LeetCode】020. Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  5. C# Hashtable赋值给另一个Hashtable时

    c#中想将一个hashtable的元素全部给另一个hashbale时, 使用迭代一个一个元素赋值 如: ammus.Clear(); IDictionaryEnumerator ie = _temp. ...

  6. scrollspy.js--bug

    /** * 20140505 14.33 ycx * scrollspy.js中存在的bug!!!---为什么ui.tabs必须在scrollspy.js中的window.onload之前执行,也就是 ...

  7. C++STL 库中set容器应用

    #include<iostream> #include<cstdio> #include<set> using namespace std; set<int& ...

  8. 问题15:如何判断字符串a是否以字符串b开头或结尾

    方法一:使用正则表达式的^和$实现 '^000':表示,只匹配字符串的开头,若开头是 '000' ,则返回 ['000'] : '000$':表示,只匹配字符串的结尾,若结尾是 '000' ,则返回 ...

  9. pycharm安装 package报错:module 'pip' has no attribute 'main'

    转自: <pycharm安装 package报错:module 'pip' has no attribute 'main'> https://www.cnblogs.com/Fordest ...

  10. c++对象导出到lua

    转自:http://www.cnblogs.com/ringofthec/archive/2010/10/26/luabindobj.html 虽然有tolua++, luabind等等, 不过自己手 ...