In the process of charging the data center, the most interesting thing is to process the combined query.
The purpose of using a three-tier architecture is to decouple it. The three-tier architecture consists of the interface layer, business logic layer, data layer, and entity. They have their respective responsibilities, for details, see my blog "how to learn and understand the three-tier architecture ". However, when dealing with the combined query, the layer-3 architecture is not so good, so how to deal with the Combined Query in the layer-3 architecture?
The following is my solution:
First, the UI Layer (interface layer ):
Code Section:
''' <Summary> ''' query student information ''' </Summary> ''' <remarks> </remarks> public class frminquirestudentinfo private cmdinquire_click (byval sender as System. object, byval e as system. eventargs) handles cmdinquire. click dim selectstring as new entity. selectstring dim binquirestudentinfo as new BLL. inquirestudentinfo dim dT as new datatable dim F1 as string 'replaces field1, Field 1 dim F2 as string' replaces field2, Field 2 dim F3 as string 'replaces field3, field 3 select case cmbfield1.text case "student ID" f1 = "ID" case "card number" f1 = "cardno" case "name" f1 = "name" case "gender" f1 = "sex "case" Major "f1 =" department "case" Grade "f1 =" Grade "case" class "f1 =" classno "case else F1 =" "End select case cmbfield2.text case "Student ID" f2 = "ID" case "card No." f2 = "cardno" case "name" f2 = "name" case "gender" f2 = "sex" case "Major" f2 = "department" case "Grade" f2 = "Grade" case "class" f2 = "classno" case else F2 = "" End select case cmbfield3.text case "student ID" F3 =" ID "case" card number "F3 =" cardno "case" name "F3 =" name "case" gender "F3 =" sex "case" professional "F3 =" department "case" grade "F3 =" Grade "case" class "F3 =" classno "case else F3 =" "End select" pass the parameter to the entity selectstring. field1 = F1 selectstring. field2 = F2 selectstring. field3 = F3 selectstring. operator1 = cmboperator1.text selectstring. operator2 = cmboperator2.text selectstring. operator3 = cmboperator3.text selectstring. researchinfo1 = trim (txtresearchinfo1.text) selectstring. researchinfo2 = trim (txtresearchinfo2.text) selectstring. researchinfo3 = trim (txtresearchinfo3.text) selectstring. combin1 = cmbcombin1.text selectstring. combin2 = cmbcombin2.text: return the query result to the table and display dt = binquirestudentinfo. inquirestudentinfo (selectstring) dgv. datasource = DT dgv. allowusertoaddrows = false End sub
The UI Layer is mainly responsible for collecting receipts and passing data to the entity class.
The bll layer is the business processing layer. It is mainly responsible for integrating business logic.
''' <Summary> ''' module ID: B18 ''' depiction: query the basic information class of a student. It is used to query the basic information of a student ''' Author: li Baoqiang ''' create dates: ''' </Summary> ''' <remarks> </remarks> public class inquirestudentinfo ''' <summary> ''' query student information ''' </Summary> ''' <Param name = "selectstring"> entity selection string </param> ''' <returns> able table </returns> ''' <remarks> </remarks> public Function inquirestudentinfo (byval selectstring as entity. selectstring) as datatable dim getinfobystring as new Dal. getinfobystring dim dT as new datatable try dt = getinfobystring. getinfobystring ("student", selectstring) return DT catch ex as exception return DT end try end functionend class
The bll layer calls the Dal layer method. Because the BLL layer corresponds to the UI Layer, The bll layer knows the name of the data table corresponding to this function. The reason why the table name is set here, this is to use one method to complete different functions. Of course, there are more than one method. I have also seen several methods, which is similar.
The Dal layer, that is, the data layer.
''' <Summary> ''' obtains information through a string. ''' </Summary> ''' <remarks> </remarks> public class getinfobystring ''' <summary> ''' obtains information through a string. ''' </Summary> ''' <Param name = "table"> database table name </param> ''' <Param name = "selectstring"> entity selection string </param> ''' <returns> datatable table </returns> ''' <remarks> author: li Baoqiang </remarks> Public Function getinfobystring (byval table as string, byval selectstring as entity. selectstring) as datatable dim helper as dbsqlhelper. sqlhelper helper = new dbsqlhelper. sqlhelper dim dT as datatable dt = new datatable dim selectstring1 as new entity. selectstring dim SQL as string SQL = selectstring. selectstring (table, selectstring) Try dt = helper. excutereader (SQL) return DT catch ex as exception return DT end try end functionend class
The data layer mainly deals with databases. Here, the Dal layer calls the object method selectstring (table, selectstring). Here, the adding method to the object is a breakthrough in processing the Combined Query. You can see how to use this method in the following entities.
Entity layer, that is, entity. (Part of code)
''' <Summary> ''' generate the selection string ''' </Summary> ''' <Param name = "table"> database table name </param> ''' <Param name = "SS"> selectstring, entity selection string </param> ''' <returns> string, SQL statement </returns> ''' <remarks> author: li Baoqiang </remarks> Public Function selectstring (byval table as string, byval ss as selectstring) as string dim SQL as string try SQL = "select * from" & table & "where" & SS. field1 & "& SS. operator1 & "'" & SS. researchinfo1 & "'" If ss. combin1 = "" Then return SQL else SQL = SQL & "" & SS. combin1 & "& SS. field2 & "& SS. operator2 & "'" & SS. researchinfo2 & "'" If ss. combin2 = "" Then return SQL else SQL = SQL & "" & SS. combin2 & "& SS. field3 & "& SS. operator3 & "'" & SS. researchinfo3 & "'" Return SQL end if catch ex as exception' error handling SQL = "" Return SQL end try end functionend class
Here, the entity is only for processing the selectstirng method.
The selectstring method is used to synthesize an SQL statement based on input parameters, including the table name. In this way, different functions can be combined for query, however, only the data queried by this function is in one table or in the same view.
The combined queries processed in this way comply with the requirements of the three-tier architecture and achieve the goal of decoupling.