C # displays the data information of multiple tables in the database hierarchically,

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.