C # displays the data information of multiple tables in the database hierarchically,
For example, how can we load the content of three tables into the same form?
To implement the above query results, we need to get the Student name from the Student table, the Subject name from the Subject table, and the test score and time from the StudentResult table.
Generally, we can write multi-table join query statements, but today we are no longer facing common development,
The reason and method for using Tiering, as we mentioned earlier, also know that each class in the Entity class corresponds to a table in the database.
The problem we face today is that the database does not contain a table (Student name, Subject name, exam score, and exam time,
So, how can we solve this problem? Today we will introduce the simplest solution of N multiple medium: Adding extension classes.
If you have learned inheritance, you can apply it here.
In this way, you can add extended fields in the newly added StudentExtens class.
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; } }}
In this way, we can query the corresponding data at the DAL layer.
// View Student Score information 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 ;}
In The BLL Layer
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(); } }}
You can call it in the UI Layer.
// Load all dgvList StudentBLL bll = new StudentBLL (); List <StudentExtens> list = bll. SelectStudentResult (); dataGridView1.DataSource = list;
Okay, the three layers have ended. At present, we have all learned simple layer-3 development, so in normal development, we may expand to multiple layers based on these three layers for business reasons.