C# 分层 三层架构
三层架构↓↓↓↓↓↓
三层架构分为:表现层(UI(User Interface))、业务逻辑层(BLL(Business Logic Layer))、数据访问层(DAL(Data Access Layer))再加上实体类库(Model)
1、实体类库(Model),主要存放数据库中的表字段。
操作:
(1)先建立实体类库Model,打开项目,在解决方案中右键——>添加——>新建项目——>选中类库——>改名Model——>确定
(2)选中Model类库——>Shift+ALT+C——>建立实体类。UserInfo类
1 namespace Model
2 {
3 public class UserInfo
4 {
5 public string UserName { get; set; }
6 public string Password { get; set; }
7 }
8 }
2、数据访问层(DAL),主要是存放对数据类的访问,即对数据库的添加、删除、修改、更新等基本操作
操作:
(1)先建立数据访问层类库DAL,打开项目,在解决方案中右键——>添加——>新建项目——>选中类库——>改名DAL——>确定
(2)在DAL中添加对Model的引用,选中DAL--》Alt+P+R--》解决方案--》项目--》选中MOdel--》确定
(3)在DAL中添加对system.configuration的引用,选中DAL——>Alt+P+R——>程序集——>框架——>选中System.configuration——>确定
(4)建立数据访问类,选中DAL——>Shift+ALT+C——>建立数据访问类。UserDB类
1 namespace DAL
2 {
3 class UserDB
4 {
5 private string connString = ConfigurationManager.ConnectionStrings[connString].ToString();
6 public int AddUser(UserInfo userInfo)
7 {
8 //对数据库进添加一个用户操作
9 string commandText = insert into UserInfo (userName,Password)values(@userName,@Password);
10 SqlParameter[] paras = new SqlParameter[]
11 {
12 new SqlParameter (@userName,userInfo.UserName ),
13 new SqlParameter (@Password,userInfo.Password )
14 };
15 return SqlHelper.ExecuteNonQuery(connString, CommandType.Text, commandText, paras);
16 }
17 }
3、业务逻辑层(BLL)对传送数据进行逻辑判断分折,并进行传送正确的值。
(1)先建立业务逻辑层类库BLL,打开项目,在解决方案中右键——>添加——>新建项目——>选中类库——>改名BLL——>确定
(2)在BLL中添加对Model、DAL的引用,选中BLL——>Alt+P+R——>解决方案——>项目——>选中MOdel、DAL——>确定
(3)建立业务逻辑类,选中BLL——>Shift+ALT+C——>建立业务逻辑类。LoginManager类
1 namespace BLL
2 {
3 public class LoginManager
4 {
5 private UserDB userDB = new UserDB();
6 public bool Add(UserInfo userInfo, out string messageStr)
7 {
8 messageStr = ;//返回界面层添加用户返回信息
9 bool isSuccess = false;
10 if (userInfo.UserName.Trim().Length != 0)//判断从传递来的username是否为空
11 {
12 if (userDB.IsEquals(userInfo))//传给DALl操作判断数据库中是否有重复值
13 {
14 userDB.AddUser(userInfo);//传给DAL操作增加一个新用户
15 isSuccess = true;
16 }
17 else
18 messageStr = 有相同的值;
19 }
20 else
21 {
22 messageStr = 不能为空;
23
24 }
25 return isSuccess;//返回界面层是否添加成功
26 }
27 }
28 }
4.表现层(UI)即用户界面层
(1)在UI中添加对Model、BLL的引用,选中UI--》Alt+P+R--》解决方案--》项目--》选中MOdel、BLL--》确定
(2)编写代码传递数据给BLL层。
1 UserInfo userInfo;
2 LoginManager lm = new LoginManager();
3 private void btnAdd_Click(object sender, EventArgs e)
4 {
5 userInfo = new UserInfo()
6 {
7 UserName = txtUserName.Text.Trim(),
8 Password = txtPassword.Text.Trim()
9 };
10 string messageStr = ;
11
12 if (lm.Add(userInfo, out messageStr))
13 {
14 MessageBox.Show(添加成功);
15 }
16 else
17 {
18 MessageBox.Show(messageStr);
19 txtUserName.Focus();
20 }
21
22 }
23 }
再来说一下怎么实现如下图所示 三个表的信息显示在一个控件
想要实现下图内容 , 首先要从学生表(Student)、从科目表(Subject)、从成绩表(StudentResult)分别拿到学生姓名、科目名、考试成绩和考试时间。

首先添加一个扩展类,这里要用到继承

namespace Combox.Model
{
public class StudentExtens:Student
{
public string SubjectName { get; set; }
public int StudentResult { get; set; }
public DateTime ExamDate { get; set; }
}
}
继续DAL层
1 public List<StudentExtens> SelectStudentResult()
2 {
3 //查看学生成绩
4 List<StudentExtens> list = new List<StudentExtens>();
5 SqlConnection con = new SqlConnection("Server=192.168.15.21;initial catalog=MySchool;uid=sa;");
6 DataTable dt = SQLHelper.ExecuteDataTable(@"select studentname,subjectname,studentresult,examdate from student,subject,result where student.studentno=result.studentno and result.subjectid=subject.subjectid");
7 foreach (DataRow item in dt.Rows)
8 {
9 StudentExtens se = new StudentExtens();
10 se.StudentName = item["studentname"].ToString();
11 se.SubjectName = item["subjectname"].ToString();
12 se.StudentResult = Convert.ToInt32(item["studentresult"]);
13 se.ExamDate = Convert.ToDateTime(item["examdate"]);
14 list.Add(se);
15 }
16 return list;
17 }
接下来是BLL层
1 namespace Combox.BLL
2 {
3 public class StudentBLL
4 {
5 StudentDAL sd = new StudentDAL();
6 public List<StudentExtens> SelectStudentResult()
7 {
8 return sd.SelectStudentResult();
9 }
10 }
11 }
在UI进行调用
1 StudentBLL sb = new StudentBLL(); 2 List<StudentExtens> list = sb.SelectStudentResult(); 3 dgvlist.DataSource = list;

浙公网安备 33010602011771号