如下图,要实现将三个表中的内容加载到同一个窗体中,该怎么来实现呢?

要实现上面的查询结果,我们就要从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#中 分层 显示数据库中多表的数据信息的更多相关文章

  1. DataGridView设置不自动显示数据库中未绑定的列

    项目中将从数据库查出来的数据绑定到DataGridView,但是不想显示所有的字段.此功能可以通过sql语句控制查出来的字段数目,但是DataGridView有属性可以控制不显示未绑定的数据,从UI层 ...

  2. 在mysql数据库中创建Oracle数据库中的scott用户表

    在mysql数据库中创建Oracle数据库中的scott用户表 作者:Eric 微信:loveoracle11g create table DEPT ( DEPTNO int(2) not null, ...

  3. Oracle中查询当前数据库中的所有表空间和对应的数据文件语句命令

    Oracle中查询当前数据库中的所有表空间和对应的数据文件语句命令 ------------------------------------------------------------------ ...

  4. Atitit. 数据库-----catalog与schema的设计区别以及在实际中使用 获取数据库所有库表 java jdbc php  c#.Net

    Atitit. 数据库-----catalog与schema的设计区别以及在实际中使用 获取数据库所有库表 java jdbc php  c#.Net 1. -catalog与schema的设计区别1 ...

  5. SqlServer中获取所有数据库,所有表,所有字段

    原文:SqlServer中获取所有数据库,所有表,所有字段 一.获取所有数据库 select * from master.dbo.SysDatabases 二.获取某个库中所有表 SELECT * F ...

  6. 页面中直接显示FTP中的图片

    页面中直接显示FTP中的图片 FTP根目录下有一张图片,如下 第一步: 通过如下格式,在浏览器上输入路径,确定可看到图片 ftp://root:root@127.0.0.1/111.png ftp:/ ...

  7. Oracle 和 MySQL 在显示数据库名和表名的区别

    Oracle 显示数据库名和表名 Oracle 查看表名: select table_name from user_tables; select table_name from dba_tables; ...

  8. SQL Server 跨服务器 不同数据库之间复制表的数据

    不同数据库之间复制表的数据的方法: 当表目标表存在时: insert into 目的数据库..表 select * from 源数据库..表 当目标表不存在时: select * into 目的数据库 ...

  9. MVC3+Linq to sql 显示数据库中数据表的数据

    1:首先创建asp.net mvc3应用程序 2:创建项目完成后 找到controllers文件鼠标右击选择添加控制器 3 为models文件夹添加一个linq to sql类文件,然后把数据库中的数 ...

随机推荐

  1. 【01】CSS3 Gradient 分为 linear-gradient(线性渐变)和 radial-gradient(径 向渐变)(转)

    CSS3 Gradient 分为 linear-gradient(线性渐变)和 radial-gradient(径 向渐变).而我们今天主要是针对线性渐变来剖析其具体的用法.为了更好的应用 CSS3 ...

  2. 《Spring in action》之装配Bean

    创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入的本质. Spring装配Bean的三种主要机制: 1.在XML中进行显示配置 2.在java中进行显示配置 3.隐式的bean发现机制和自动 ...

  3. [CH#56]过河(贪心)

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2356%20-%20%E5%9B%BD%E5%BA%86%E8%8A%82%E6%AC%A2%E4%B9%90% ...

  4. Java使用JNA调用DLL库

    Java调用DLL方法有三种,JNI.JNA.JNative, 本文为JNA JNA为使用jna.jar包,下载地址:http://www.java2s.com/Code/Jar/j/Download ...

  5. 为什么说Ubuntu的运行级别为2

    继上一篇文章http://www.cnblogs.com/EasonJim/p/7163069.html深入研究了Linux的运行级别之后,发现网上大部分都说Ubuntu的运行级别默认为2,那么下面就 ...

  6. 【翻译自mos文章】使用aum( Automatic Undo Management) 时遇到 ORA-01555错误--- 原因和解决方式。

    使用aum( Automatic Undo Management) 时遇到 ORA-01555错误--- 原因和解决方式. 參考原文: ORA-01555 Using Automatic Undo M ...

  7. python-paramiko初体验

    什么pexpect.pxshll在paramiko面前都是浮云,重要的是paramiko支持windows. 小试牛刀 import paramiko paramiko.util.log_to_fil ...

  8. Centos 7 nginx-1.12.0 配置学习(一)

    [root@bogon nginx]# vim nginx.conf #user nobody; #运行用户 worker_processes ; #启动进程,通常设置成和cpu核心数相等 #全局错误 ...

  9. 运行Java -jar somefile.jar时发生了什么(二)

    (6)Java.c中的LoadMainClass 位置jdk/src/share/bin/java.c 该方法负责载入main函数所在的类. 该方法首先载入sun.launcher.LauncherH ...

  10. 处理new分配内存失败情况

    转自:http://www.51testing.com/html/70/n-827070.html 在C++语言中,我们经常会使用new给一个对象分配内存空间,而当内存不够会出现内存不足的情况.C++ ...