《简单三层程序设计.doc》由会员分享,可在线阅读,更多相关《简单三层程序设计.doc(9页珍藏版)》请在金锄头文库上搜索。
1、三层结构包含:表示层(USL),业务逻辑层(BLL),数据访问层(DAL)1:数据访问层:主要是对原始数据(数据库或者文本文件等存放数据的形式)的操作层,而不 是指原始数据,也就是说,是对数据的操作,而不是数据库,具体为业务逻辑层或表示层提供数据服务2:业务逻辑层:主要是针对具体的问题的操作,也可以理解成对数据层的操作,对数据业务逻 辑处理,如果说数据层是积木,那逻辑层就是对这些积木的搭建。3:表示层:主要表示WEB方式,也可以表示成WINFORM方式, 如果逻辑层相当强大和完善,无论表现层如何定义和更改,逻辑层都能完善地提供服务。具体的区分方法1:数据访问层:主要看你的数据层里面有没有包含逻
2、辑处理,实际上他的各个函数主要完成 各个对数据文件的操作。而不必管其他操作。2:业务逻辑层:主要负责对数据层的操作。也就是说把一些数据层的操作进行组合。3:表示层:主要对用户的请求接受,以及数据的返回,为客户端提供应用程序的访问。三层结构说明完善的三层结构的要求是:修改表现层而不用修改逻辑层,修改逻辑层而不用修改数据层.否则你的应用是不是多层结构,或者说是层结构的划分和组织上是不是有问题就很难说.不同的应用有不同的理解,这是一个概念的问题流程图 部署三层结构1:新建一空白解决方案2:在此解决方案上添加新建项目类库 取名DBEntity(数据库实体)3:在此解决方案上添加新建项目类库 取名DAL
3、(数据访问层)4:在次解决方案上添加新建项目类库 取名BLL(业务逻辑层)5:在次解决方案上添加新建网站ASP.NET网站 取名WebSite(表示层,WinForm项目的话添加一Window应用程序)6:DAL,BLL, WebSite分别添加对数据库实体DBEntity的引用7:BLL添加对对DAL的引用,WebSite添加对BLL的引用下面用一用户登陆演示项目DBEntity添加UserInfo.cs,代表数据库实体,一般是和数据库一一对应的view source print?01using System; 02using System.Collections.Generic; 03us
4、ing System.Text; 04namespace DBEntity 05 06public class UserInfo 07 08private int _id; 09private string _userName; 10private string _passWord; 11public int Id 12 13get return _id; 14set _id = value; 15 16public string UserName 17 18get return _userName; 19set _userName = value; 20 21public string Pa
5、ssWord 22 23get return _passWord; 24set _passWord = value; 25 26 27DAL里添加UserDAL.csview source print?01using System; 02using System.Data; 03using System.Data.SqlClient; 04using System.Configuration; 05using System.Collections.Generic; 06using DBEntity; 07namespace DAL 08 09public class UserDAL 10 11
6、private string ConnectionString = ConfigurationManager.AppSettingsConnectionString.ToString(); 12public UserInfo Login(string userName, string passWord) 13 14UserInfo info = new UserInfo(); 15string strSql = select id,userName,passWord from Users where userName=userName and passWord=passWord; 16SqlC
7、onnection conn = new SqlConnection(ConnectionString); 17conn.Open(); 18SqlCommand com = new SqlCommand(); 19com.CommandType = CommandType.Text; 20com.CommandText = strSql; 21com.Connection = conn; 22com.Parameters.AddWithValue(userName, userName); 23com.Parameters.AddWithValue(passWord, passWord); 2
8、4SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection); 25if (dr.Read() 26 27info.Id = Convert.ToInt32(drid); 28info.UserName = druserName.ToString(); 29info.PassWord = drpassWord.ToString(); 30return info; 31 32else33 34return null; 35 36 37 38BLL里添加UserBLL.csview source print?01usi
9、ng System; 02using System.Collections.Generic; 03using System.Text; 04using DBEntity; 05using DAL; 06namespace BLL 07 08public class UserBLL 09 10UserDAL dal = new UserDAL(); 11public UserInfo Login(string userName, string passWord) 12 13return dal.Login(userName, passWord); 14 15 16Web里Login.aspx对应
10、的后台代码view source print?01using System; 02using BLL; 03using DBEntity; 04public partial class _Default : System.Web.UI.Page 05 06protected void Page_Load(object sender, EventArgs e) 07 08 09protected void Button1_Click(object sender, EventArgs e) 10 11UserBLL data = new UserBLL(); 12UserInfo info = new UserInfo(); 13info = data.Login(TextBox1.Text, TextBox2.Text); 14if (info != null) 15 16/登陆成功 17Response.Write(alert(OK!); 18 19else20 21/登陆失败 22Response.Write(alert(ERROR!); 23 24 25至此,简单的三层架构用户登陆完成了!