C#中 分层 显示数据库中多表的数据信息
如下图,要实现将三个表中的内容加载到同一个窗体中,该怎么来实现呢?

要实现上面的查询结果,我们就要从Student表中拿到学生姓名,从Subject表中拿到科目名称,从StudentResult表中拿到考试成绩和考试时间。
一般情况下我们都能够写出多表联查的语句来,但是今天我们所面临的不再是普通的开发,
而使用分层的原因和方法,我们在前面以及提到过,也知道了实体类中的每个类都是对应与数据库中的一张表。
那么今天我们所面临的问题是,在数据库中并没有包含(学生姓名,科目名称,考试成绩和考试时间)的一张表,
那么,我们又如何来解决这种问题呢,今天就来介绍N多中解决方案中,最简单的一种:添加扩展类。
已经学习过继承的我们,就可以在这里进行应用了。

就像这样 ,在新添加的StudentExtens类中就可以添加扩展的字段了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Combox.Model
{
public class StudentExtens:Student
{
public string SubjectName { get; set; }
public int StudentResult { get; set; }
public DateTime ExamDate { get; set; }
}
}
这样,我们就可以在DAL层来实现查询相应的数据了
//查看学生成绩信息
public List<StudentExtens> SelectStudentResult()
{
List<StudentExtens> list = new List<StudentExtens>();
SqlConnection con = new SqlConnection("Server=192.168.100.100;initial catalog=MySchool;uid=sa;pwd=1");
DataTable dt = SQLHelper.ExecuteDataTable(@"select studentname,subjectname,studentresult,examdate from student,subject,result where student.studentno=result.studentno and result.subjectid=subject.subjectid");
foreach (DataRow item in dt.Rows)
{
StudentExtens se = new StudentExtens();
se.StudentName = item["studentname"].ToString();
se.SubjectName = item["subjectname"].ToString();
se.StudentResult = Convert.ToInt32(item["studentresult"]);
se.ExamDate = Convert.ToDateTime(item["examdate"]);
list.Add(se);
}
return list;
}
在BLL层中
using Combox.DAL;
using Combox.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Combox.BLL
{
public class StudentBLL
{
StudentDAL dal = new StudentDAL();
public List<StudentExtens> SelectStudentResult()
{
return dal.SelectStudentResult();
}
}
}
在UI层中就可以进行调用了
//加载所有的dgvList
StudentBLL bll = new StudentBLL();
List<StudentExtens> list = bll.SelectStudentResult();
dataGridView1.DataSource = list;
ok,三层到此结束,目前我们所学皆为浅显的三层开发,那么在正常的开发中可能会因为业务原因,基于这三层去扩展跟多的层面。
C#中 分层 显示数据库中多表的数据信息的更多相关文章
- DataGridView设置不自动显示数据库中未绑定的列
项目中将从数据库查出来的数据绑定到DataGridView,但是不想显示所有的字段.此功能可以通过sql语句控制查出来的字段数目,但是DataGridView有属性可以控制不显示未绑定的数据,从UI层 ...
- 在mysql数据库中创建Oracle数据库中的scott用户表
在mysql数据库中创建Oracle数据库中的scott用户表 作者:Eric 微信:loveoracle11g create table DEPT ( DEPTNO int(2) not null, ...
- Oracle中查询当前数据库中的所有表空间和对应的数据文件语句命令
Oracle中查询当前数据库中的所有表空间和对应的数据文件语句命令 ------------------------------------------------------------------ ...
- Atitit. 数据库-----catalog与schema的设计区别以及在实际中使用 获取数据库所有库表 java jdbc php c#.Net
Atitit. 数据库-----catalog与schema的设计区别以及在实际中使用 获取数据库所有库表 java jdbc php c#.Net 1. -catalog与schema的设计区别1 ...
- SqlServer中获取所有数据库,所有表,所有字段
原文:SqlServer中获取所有数据库,所有表,所有字段 一.获取所有数据库 select * from master.dbo.SysDatabases 二.获取某个库中所有表 SELECT * F ...
- 页面中直接显示FTP中的图片
页面中直接显示FTP中的图片 FTP根目录下有一张图片,如下 第一步: 通过如下格式,在浏览器上输入路径,确定可看到图片 ftp://root:root@127.0.0.1/111.png ftp:/ ...
- Oracle 和 MySQL 在显示数据库名和表名的区别
Oracle 显示数据库名和表名 Oracle 查看表名: select table_name from user_tables; select table_name from dba_tables; ...
- SQL Server 跨服务器 不同数据库之间复制表的数据
不同数据库之间复制表的数据的方法: 当表目标表存在时: insert into 目的数据库..表 select * from 源数据库..表 当目标表不存在时: select * into 目的数据库 ...
- MVC3+Linq to sql 显示数据库中数据表的数据
1:首先创建asp.net mvc3应用程序 2:创建项目完成后 找到controllers文件鼠标右击选择添加控制器 3 为models文件夹添加一个linq to sql类文件,然后把数据库中的数 ...
随机推荐
- Shiro_DelegatingFilterProxy
1.DelegatingFilterProxy实际上是Filter的一个代理对象.默认情况下,Spring会到IOC容器中查找与<filter-name>对应的filter bean.也可 ...
- iOS攻城狮修炼之路
自己总结的学习iOS的笔记,打造一个全面的知识体系,iOS攻城狮修炼之路[持续更新中] iOS学习笔记01-APP相关 iOS学习笔记02-UIScrollView iOS学习笔记03-UITable ...
- windows安装Reids
1.下载windows版本,64位,3.0版本就可以 官网下载地址:http://redis.io/download github下载地址:https://github.com/MSOpenTech/ ...
- 中庸之道(codevs 2021)
题目描述 Description 给定一个长度为N的序列,有Q次询问,每次询问区间[L,R]的中位数. 数据保证序列中任意两个数不相同,且询问的所有区间长度为奇数. 输入描述 Input Descri ...
- [codeVS3943] 数学奇才琪露诺
题目描述 Description 作为上白泽慧音老师的出色弟子,数学奇才琪露诺在算术方面有很深的造诣.今天,codevs有幸请到了这位数学界的奇葩作为本场考试的第一题主考官. 琪露诺喜欢0-9之间的数 ...
- HashMap源码分析3:移除
本文源码基于JDK1.8.0_45. final Node<K,V> removeNode(int hash, Object key, Object value, boolean matc ...
- Maticsoft Code Generator
源码:https://github.com/easonjim/MaticsoftCodeGenerator bug提交:https://github.com/easonjim/MaticsoftCod ...
- I/O 模型及其设计模式
来源:伯乐在线 - 咸菜 链接:http://blog.jobbole.com/104638/ 前言 I/O在软件开发中的重要性无需多言,无论是在操作系统.网络协议.DBMS这种底层支撑软件还是在移动 ...
- Install nginx-clojure on CentOS 7
Install nginx-clojure on CentOS 7 1. install open-jdk-7 sudo yum install java-1.7.0-openjdk-devel 2. ...
- Java - this的使用方法
this在内部获得当前对象的引用时调用: (1) return返回当前对象; (2) 构造器调用还有一个构造器, 带參数; (3) 參数的名称和数据成员的名称同样; 注意: this构造器在方法中仅仅 ...