生活随笔
收集整理的這篇文章主要介紹了
三层架构实例
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
三層架構(gòu)實(shí)例
一、新建立一個(gè)三層架構(gòu)體系結(jié)構(gòu)解決方案
新建一個(gè)空白解決方案。然后:????
1.“添加”-“新建項(xiàng)目”-“Visual C#”-“類庫”-“稱:LoginBLL”-“業(yè)務(wù)邏輯”(業(yè)務(wù)層,下簡(jiǎn)稱B層)如下圖:
?2.“添加”-“新建項(xiàng)目”-“Visual C#”-“類庫”-“名稱:LoginDAL”-“數(shù)據(jù)訪問”(數(shù)據(jù)層,下簡(jiǎn)稱D層)如下圖:
3.“添加”-“新建項(xiàng)目”-“Visual C#”-“類庫”-“名稱:LoginUI”(界面層,下簡(jiǎn)稱U層)如下圖:
U層的界面效果圖:如下圖
?4. “添加”-“新建項(xiàng)目”-“Visual C#”-“類庫”-“名稱:LoginModel”(以下實(shí)體層)如下圖:
三層建立后的效果圖,如下圖:
二、引用(關(guān)鍵步驟)
右鍵點(diǎn)“解決方案”-“添加引用”,設(shè)置U依引用D、B,B引用于D。對(duì)U添加引用D、B,對(duì)B添加引用D。如下圖:
引用步驟一:
引用步驟二:
到此為止,一個(gè)登錄的三層架構(gòu)的建立起來了。
三、各層實(shí)現(xiàn)代碼如下
B層代碼
[csharp] view plaincopyprint?
using?System;??using?System.Collections.Generic;??using?System.Linq;??using?System.Text;??using?System.Threading.Tasks;??using?Login.BLL;??using?Login.DAL;??using?Login.Model;??namespace?Login.BLL??{??????public?class?LoginManager??????{??????????public?UserInfo?UserLogin(string?userName,?string?password)??????????{????????????????UserDAO?uDao?=?new?UserDAO();????????????????UserInfo?user?=?uDao.SelectUser(userName,?password);???????????????if?(user?==?null)????????????????{??????????????????throw?new?Exception("登陸失敗");??????????????}??????????????else??????????????{??????????????????return?user;??????????????}??????????????}??????}??}??
D層代碼
[csharp] view plaincopyprint?
using?System;??using?System.Collections.Generic;??using?System.Linq;??using?System.Text;??using?System.Threading.Tasks;??using?Login.Model;??using?System.Data.SqlClient;??using?System.Data;????namespace?Login.DAL??{??????public?class?UserDAO??????{?????????????????????public?static?string?ConnString?=?@"Server=XJC-PC;?Database=chargeStudent;?User?ID=sa;?Password=123456";???????????????????public??UserInfo?SelectUser(string?userName,?string?password)?????????????{????????????????????????????using?(SqlConnection?conn?=?new?SqlConnection(ConnString))??????????????{??????????????????SqlCommand?cmd?=?conn.CreateCommand();???????????????????cmd.CommandText?=?@"select?*?from?user_Info?where?user_Name=@userName?AND?user_PWD=@password";??????????????????cmd.CommandType?=?CommandType.Text;??????????????????cmd.Parameters.Add(new?SqlParameter("@userName",?userName));??????????????????cmd.Parameters.Add(new?SqlParameter("@password",?password));??????????????????conn.Open();??????????????????SqlDataReader?reader?=?cmd.ExecuteReader();????????????????????UserInfo?user?=?new?UserInfo();??????????????????while?(reader.Read())??????????????????{????????????????????????????????????????????user.userName?=?reader.GetString(0);??????????????????????user.password?=?reader.GetString(1);???????????????????????????????????????}????????????????????return?user;??????????????}??????????????????????}????????}??}??
U層代碼
[csharp] view plaincopyprint?
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?System.Data.SqlClient;??using?Login.BLL;??using?Login.Model;??namespace?LoginUI??{??????public?partial?class?Form1?:?Form??????{??????????public?Form1()??????????{??????????????InitializeComponent();??????????}????????????private?void?btnLogin_Click(object?sender,?EventArgs?e)??????????{??????????????LoginManager?mgr?=?new?LoginManager();???????????????string?userName?=?txtUserName.Text.Trim();??????????????string?Password?=?txtPassword.Text;??????????????UserInfo?user?=?mgr.UserLogin(userName,?Password);?????????????????MessageBox.Show("登陸用戶:"?+?userName);??????????}??????}??}??
實(shí)體層代碼
[csharp] view plaincopyprint?
using?System;??using?System.Collections.Generic;??using?System.Linq;??using?System.Text;??using?System.Threading.Tasks;????namespace?Login.Model??{??????public?class?UserInfo??????{??????????public?string?userName;??????????public?string?UserName??????????{??????????????get?{?return?userName;?}??????????????set?{?userName?=?value;?}??????????}??????????public?string?password;??????????public?string?PassWord??????????{??????????????get?{?return?password;?}??????????????set?{?password?=?value;?}??????????}??????}??}??
四、運(yùn)行成功的結(jié)果圖:如下圖
以上是我對(duì)“三層實(shí)例”的真是實(shí)現(xiàn),如有不妥之處,還請(qǐng)指教。
總結(jié)
以上是生活随笔為你收集整理的三层架构实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。