试想:如果不使用数据库中的 view 视图,那么在三层结构的开发环境下,如何实现两表或多表( join table )联接查询所需要的显示结果呢?
为了直观揭露实现办法的本质,于是在此弱化了一些操作步骤,如:配置文件保存连接字符串、数据访问类( DBHelper.cs )访问数据库等的具体实现。
在本示例的数据库中:学生信息表(Students)与成绩表(Score)是一对多的关系( 1:N )。
实体层(Entity):
Student.cs
Student.cs
Score.cs
Score.cs
数据访问层(DAL):
StudentDAL.cs
StudentDAL.cs
ScoreDAL.cs
ScoreDAL.cs
业务逻辑层(BLL):
StudentAndScore.cs
StudentAndScore.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace BLL
{
/// <summary>
/// 封装一个包含2张表字段的实体
/// </summary>
public class StudentAndScore
{
int code;
public int Code
{
get { return code; }
set { code = value; }
}
string name;
public string Name
{
get { return name; }
set { name = value; }
}
float? stuScore;
public float? StuScore
{
get { return stuScore; }
set { stuScore = value; }
}
}
}
using System.Collections.Generic;
using System.Text;
namespace BLL
{
/// <summary>
/// 封装一个包含2张表字段的实体
/// </summary>
public class StudentAndScore
{
int code;
public int Code
{
get { return code; }
set { code = value; }
}
string name;
public string Name
{
get { return name; }
set { name = value; }
}
float? stuScore;
public float? StuScore
{
get { return stuScore; }
set { stuScore = value; }
}
}
}
StudentAndTeacherBLL.cs
StudentAndTeacherBLL.cs
using System;
using System.Collections.Generic;
using System.Text;
//myself
using DAL;
using Entity;
namespace BLL
{
/// <summary>
/// 获得2张表对应的数据
/// </summary>
public class StudentAndTeacherBLL
{
/// <summary>
/// 注意:该方法获得所有学生的成绩,
/// 属于“左外联接”(当没有成绩时,成绩显示为空)。
/// 若要实现“内联接”的显示效果,
/// 则去掉:if(stuScores.Count == 0) 的判断即可!
/// </summary>
/// <returns>2张表数据的结果集</returns>
public List<StudentAndScore> GetStudentAndTeacher()
{
//用来保存结果集
List<StudentAndScore> allStudentAndScore = new List<StudentAndScore>();
//获得所有学生的信息
List<Student> students = new StudentDAL().GetAllStudent();
foreach (Student stu in students)
{
//根据学号获得该学生的所有成绩
List<StuScore> stuScores = new ScoreDAL().GetAllScore(stu.Code);
if (stuScores.Count == 0) //如果没有该学生的成绩
{
//用来保存单个联接实体
StudentAndScore studentAndScore = new StudentAndScore();
studentAndScore.Code = stu.Code;
studentAndScore.Name = stu.Name;
studentAndScore.StuScore = null; //成绩为null
//添加到结果集
allStudentAndScore.Add(studentAndScore);
}
else //如果有该学生的成绩
{
foreach (StuScore SScore in stuScores)
{
//用来保存单个联接实体
StudentAndScore studentAndScore = new StudentAndScore();
studentAndScore.Code = stu.Code;
studentAndScore.Name = stu.Name;
studentAndScore.StuScore = SScore.Score;
//添加到结果集
allStudentAndScore.Add(studentAndScore);
}
}
}
return allStudentAndScore; //返回结果集
}
}
}
using System.Collections.Generic;
using System.Text;
//myself
using DAL;
using Entity;
namespace BLL
{
/// <summary>
/// 获得2张表对应的数据
/// </summary>
public class StudentAndTeacherBLL
{
/// <summary>
/// 注意:该方法获得所有学生的成绩,
/// 属于“左外联接”(当没有成绩时,成绩显示为空)。
/// 若要实现“内联接”的显示效果,
/// 则去掉:if(stuScores.Count == 0) 的判断即可!
/// </summary>
/// <returns>2张表数据的结果集</returns>
public List<StudentAndScore> GetStudentAndTeacher()
{
//用来保存结果集
List<StudentAndScore> allStudentAndScore = new List<StudentAndScore>();
//获得所有学生的信息
List<Student> students = new StudentDAL().GetAllStudent();
foreach (Student stu in students)
{
//根据学号获得该学生的所有成绩
List<StuScore> stuScores = new ScoreDAL().GetAllScore(stu.Code);
if (stuScores.Count == 0) //如果没有该学生的成绩
{
//用来保存单个联接实体
StudentAndScore studentAndScore = new StudentAndScore();
studentAndScore.Code = stu.Code;
studentAndScore.Name = stu.Name;
studentAndScore.StuScore = null; //成绩为null
//添加到结果集
allStudentAndScore.Add(studentAndScore);
}
else //如果有该学生的成绩
{
foreach (StuScore SScore in stuScores)
{
//用来保存单个联接实体
StudentAndScore studentAndScore = new StudentAndScore();
studentAndScore.Code = stu.Code;
studentAndScore.Name = stu.Name;
studentAndScore.StuScore = SScore.Score;
//添加到结果集
allStudentAndScore.Add(studentAndScore);
}
}
}
return allStudentAndScore; //返回结果集
}
}
}
用户界面层(UI):
Form1.cs
Form1.cs
显示结果如下:
说明:Code、Name 字段来自信息表(Students),而StuScore 字段来自成绩表(Score)。
本文转自钢钢博客园博客,原文链接:http://www.cnblogs.com/xugang/archive/2010/05/08/1730388.html,如需转载请自行联系原作者