三层结构之联接查询的实现

简介:

试想:如果不使用数据库中的 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

复制代码
ExpandedBlockStart.gif 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; }
        }
    }
}
复制代码


StudentAndTeacherBLL.cs

复制代码
ExpandedBlockStart.gif 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;  // 返回结果集
        }
    }
}
复制代码



用户界面层(UI):

Form1.cs

Form1.cs

显示结果如下:

studentANDscore

说明:Code、Name  字段来自信息表(Students),而StuScore 字段来自成绩表(Score)。




本文转自钢钢博客园博客,原文链接:http://www.cnblogs.com/xugang/archive/2010/05/08/1730388.html,如需转载请自行联系原作者


相关文章
queryDsl多表联合查询,任意查询联合字段
这是我的通用方法 package com.spark.bitrade.service.Base; import com.querydsl.core.Tuple; import com.
4057 0
|
2天前
|
存储 SQL 关系型数据库
SQL联结表及高级联结
SQL联结表及高级联结
33 0
|
12月前
|
算法 索引
关系查询处理和查询优化
关系查询处理和查询优化
|
数据库
子查询与内连接查询区别(效率上,连接查询高于子查询)、左连接以及连接的原理,还有内连接与左连接的区别
子查询与内连接查询区别(效率上,连接查询高于子查询)、左连接以及连接的原理,还有内连接与左连接的区别
1050 0
子查询与内连接查询区别(效率上,连接查询高于子查询)、左连接以及连接的原理,还有内连接与左连接的区别
|
关系型数据库 MySQL 数据库
MySQL数据库:第十一章:合并查询(联合查询)
MySQL数据库:第十一章:合并查询(联合查询)
|
SQL 数据库
基础DQL(数据查询)——多表关系及多表查询概述
我们要想在数据库的层面上体现这种关系,就要建立第三张中间表,中间表至少包含两个外键,分别关联两方主键
84 0
基础DQL(数据查询)——多表关系及多表查询概述
|
SQL 数据库
数据库查询——组合表查询
数据库查询——组合表查询
127 0
|
SQL 安全 数据库
【T-SQL基础】02.联接查询
【T-SQL基础】02.联接查询
151 0
【T-SQL基础】02.联接查询
|
SQL 关系型数据库 MySQL
跨表查询经常有,何为跨表更新?
跨表查询经常有,何为跨表更新?
跨表查询经常有,何为跨表更新?