An excellent program for apes is to minimize the amount of code they have. In our daily work or study it is possible to encounter multiple window structures similar, or the same situation, when it is assumed that every window is written in code, or pure copy and paste is too water. To be lazy is the opportunity we find and create, and so does our ancestors.
I am doing the computer room toll system personal reconstruction version, encountered the basic data maintenance, check the machine status, computer information statistics and operator work record four windows are used to combine query and window structure consistent.
Four windows in addition to the field name is inconsistent, the other content is the same, so we use the window to inherit the most appropriate. Write a common section in the parent window, where inconsistencies can write a virtual method, and then let the subclass override the virtual method of the parent class. So we just need a U-layer, a B-layer, an interface, an abstract factory, a D-layer and then the subclass rewrite the virtual method to achieve the functions of the four windows.
The U-layer mainly infers whether the control is empty, gives the value of the entity parameters and the B-layer method, and defines the virtual method to convert the database fields and the virtual methods to get the table names, which are overridden in different subclasses to implement different functions.
' ************************************************* ' Shao ' group: ' Description: Combination query template ' Date Created: 2014.7.9 ' version: ' *********************** /public Class frmgroupquery public thisgroupquery as New Entity.groupqueryenpty ' defines an entity parameter Pri vate Sub button1_click_1 (sender as Object, e as EventArgs) Handles Btnquery.click ' first row combination is empty If cmbRelation1 . Text = "Then ' infers whether the first row of control content is empty if Cmbfield1.text =" Then MsgBox ("Enter field name") ElseIf Cmboperation1.text = "Then MsgBox (" Enter operator ") ElseIf Txtcontent1.text =" Then MsgBox ("Please enter what you want to query") End If End If ' when the first composition relationship is not empty if Cmbrelation1.text <> "" Then If cmbfield1.text = "and then MsgBox (" Enter field name ") ElseIf Cmboperation1.text =" "The N MsgBox ("enter operator") ElseIf Txtcontent1.text = "Then MsgBox (" Please enter what you want to query ") ElseIf Cmbfield2.text = "" ThEn MsgBox ("enter field name") ElseIf Cmboperation2.text = "Then MsgBox (" Enter operator ") ElseIf Txtcontent2.text = "Then MsgBox (" Enter what you want to query ") End If End If ' when the second composition relationship does not Null if Cmbrelation2.text <> "then If cmbfield1.text =" and then MsgBox ("Enter field name") ElseIf Cmboperation1.text = "Then MsgBox (" Enter operator ") ElseIf Txtcontent1.text =" "The N MsgBox ("Enter what you want to query") ElseIf Cmbfield2.text = "Then MsgBox (" Enter field name ") ElseIf Cmboperation2.text = "Then MsgBox (" Enter operator ") ElseIf Txtcontent2.text =" Then MsgBox ("Please enter what you want to query") ElseIf Cmbfield3.text = "Then MsgBox (" Enter field name ") ElseIf cm Boperation3.text = "Then MsgBox (" Enter operator ") ElseIf Txtcontent3.text =" then MSG Box ("Please enter what you want to query") End If End If ' Pass to entity layer Dim Thisgroupquery as New entity.groupqueryenpty thisgroupquery . Cmbfield1_text = Getdbname (CmbField1.Text.Trim ()) Thisgroupquery.cmbfield2_text = Getdbname (CmbField2.Text.Trim ()) Thisgroupquery.cmbfield3_text = Getdbname (CmbField3.Text.Trim ()) Thisgroupquery.cmboperation1_text = Cmbope Ration1. Text.trim () Thisgroupquery.cmboperation2_text = CmbOperation2.Text.Trim () thisgroupquery.cmboperation3_text = CmbOperation3.Text.Trim () Thisgroupquery.txtcontent1_text = TxtContent1.Text.Trim () thisgroupquery.txtcont Ent2_text = TxtContent2.Text.Trim () Thisgroupquery.txtcontent3_text = TxtContent3.Text.Trim () thisgroupquery . Cmbrelation1_text = Getdbname (CmbRelation1.Text.Trim ()) Thisgroupquery.cmbrelation2_text = Getdbname (cmbRelation2. Text.trim ()) thisgroupquery. GetTable = GetTable () ' passes the parameter Dim db as New BLL to the B-layer Groupquery method. GROUPQUERYBLL If db. GRoupquery (Thisgroupquery) is nothing and then MsgBox ("No records, please set query conditions again", vbOKOnly, vbexclamation) Datagri Dview1.datasource = Nothing Else Datagridview1.datasource = db. Groupquery (thisgroupquery) End If End Sub ' Define virtual function getdbname, get field names for different databases Protected Overridable function Ge Tdbname (ByVal Control As String) As String Return "End function" defines the virtual function getdbname, which gets the table names of the different databases Protected Overridable function GetTable () as String Return "" End Function Private Sub button2_click_1 (sender as Object , e as EventArgs) Handles Btnexit.click me.hide () End Sub ' combined relationship one is not empty, the second row of query conditions is displayed Private Sub cmbrelation1_se Lectedindexchanged (sender as Object, e as EventArgs) Handles cmbrelation1.selectedindexchanged cmbfield2.enabled = True cmboperation2.enabled = True cmbrelation2.enabled = True txtcontent2.enabled = True End Sub ' Combined relationship two is not empty, display the third row of query conditions Private Sub cmbrelation2_selectedindexcHanged (sender as Object, e as EventArgs) Handles cmbrelation2.selectedindexchanged cmbfield3.enabled = True Cmboperation3.enabled = True txtcontent3.enabled = True End subend Class
The B-layer is written in the same way that it used to be, referencing factories and interfaces returns the table to which it was queried.
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > ' ************************************************* ' Shao ' group: ' Description: Composite query layer B ' Date created: 2014.7.17 ' version: ' ************* /imports idalimports entitypublic Class groupquerybll public Function Groupquery (ByVal queries as Entity.groupqueryenpty) as DataTable Dim factory as New factory.groupqueryfactory Dim Igroupquery as Igroupquery = Factory. Groupquery Dim table = igroupquery. Groupquery (queries) ' checks that the data table returned by the D layer has data if table. Rows.Count = 0 Then return nothing Else return table end If End FunctionEnd class</span>
The D layer implements the interface and calls the stored procedure to implement access to the database.
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > ' ************************************************* ' Shao ' group: ' Description: Composite query D-tier ' date Created: 2014.7.17 ' version: ' ****************** /imports System.Data.SqlClientImports idalpublic Class sqlservergroupquerydal: Implements igroupquery Function groupquery (ByVal queries as Entity.groupqueryenpty) as DataTable Implements igroupquery . Groupquery Dim strSQL As String = "Proc_groupquery" ' calls the stored procedure Dim prams As SqlParameter () = {New SqlParameter ( "@cmbField1", Queries.cmbfield1_text), New SqlParameter ("@cmbField2", queries.cmbfield2 _text), New SqlParameter ("@cmbField3", Queries.cmbfield3_text), New SqlParameter ("@cmbOperation1", Queries.cmboperation1_text), new Sqlpara Meter ("@cmbOperation2", Queries.cmboperation2_text), New SqlParameter ("@cmbOPeration3 ", Queries.cmboperation3_text), New SqlParameter (" @txtContent1 ", Queries.txtco Ntent1_text), New SqlParameter ("@txtContent2", Queries.txtcontent2_text), New SqlParameter ("@txtContent3", Queries.txtcontent3_text), new S Qlparameter ("@cmbRelation1", Queries.cmbrelation1_text), New SqlParameter ("@cmbRelation 2 ", Queries.cmbrelation2_text), New SqlParameter (" @tableName ", queries. GetTable)} ' Set parameters Dim Helper As New SqlHelper Dim table As new DataTable table = Helper. Getdatatable (strSQL, CommandType.StoredProcedure, prams) Return table End functionend class</span>
Stored Procedures
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >--=============================================--Author: Shao--Create date:20140719--Description: Combination Query--====== =======================================alter PROCEDURE [dbo]. [proc_groupquery]--Add the parameters for the stored procedure here@cmbfield1 varchar, @cmbOperation1 varchar (10), @ TxtContent1 varchar, @cmbField2 varchar, @cmbOperation2 varchar (TEN), @txtContent2 varchar, @cmbField3 varchar (10), @cmbOperation3 varchar, @txtContent3 varchar (TEN), @cmbRelation1 varchar, @cmbRelation2 varchar ), @tableName varchar (asdeclare) @TempSql varchar (500)--temporarily holds the SQL statement beginset @TempSql = ' SELECT * from ' [email Protected] + ' WHERE ' [email protected] [Email protected]+char (in) + @txtContent1 + char (all) if @cmbRelatio N1! = "BEGIN SET @[email protected][email protected]+char (+) [email protected] [EMAIL&NBSP;PR Otected]+char [Email protected]+char (All) if @cmbrelation2!= "BEGIN SET @[email protected][email protected]+char (+) [Email protected][ema Il protected]+char (Email protected]+char) end end EXECUTE (@TempSql) end</span>
Abstract Factory application configuration file for flexible database replacement.
<span style = "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > ' ************************************************* ' Shao ' group: ' Description: Combinatorial query abstract factory ' Date Created: 2014.7.17 ' version: ' **************** /imports system.reflectionimports system.configurationimports idalpublic Class Groupqueryfactory Private shared ReadOnly assemblyname as String = "DAL" ' declares the assembly name Private shared ReadOnly DB as Stri ng = configurationmanager.appsettings ("db") ' read config file to DB public Function groupquery () as Igroupquery Dim ClassName As String = AssemblyName + "." + db + "Groupquerydal" Dim igroupquery as Igroupquery igroupquery = CType (AS sembly. Load (AssemblyName). CreateInstance (ClassName), igroupquery) Return igroupquery End functionend class</span>
Subclass Code takes the operator's work record as an example, first joins the inheritance window
Then select the inherited window
This is followed by overriding the virtual method of the parent class
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > ' ************************************************* ' Shao ' group: ' Description: query operator work record U-layer ' Date Created: 2014.7.18 ' version: ' ************* /public Class Frmworklog ' overrides convert to Database field method Protected Overrides Function getdbname (ByVal Control as String) As String Select Case (Control) case "teacher" Return "UserName" Case "Work Date" return "logintime" case "work Time" return "logintime" case " Off date "return" Offtime "case" Time "return" Offtime "case" machine number " Return "Computer" case "with" return "and" case "or" return "or" Case Else Return "End Select End Function ' Overrides get Table name method Protected Overrides Functio n GetTable () as String thisgroupquery. GetTable = "Work_info" Return thisgroupquery. GEttable End Function public Sub frmworklog_load (sender as Object, e as EventArgs) Handles MyBase.Load ' assigns an initial value to the field CMBFIELD1.ITEMS.ADD ("Teacher") CmbField1.Items.Add ("Work Date") CmbField1.Items.Add ("Office hours") cmbfield1.i TEMs. ADD ("Off date") CmbField1.Items.Add ("Off Time") CmbField1.Items.Add ("Machine number") CmbField2.Items.Add ("Teacher") c MBFIELD2.ITEMS.ADD ("Work Date") CmbField2.Items.Add ("Work Hours") cmbField2.Items.Add ("Off date") cmbField2.Items.Ad D ("Off Hours") cmbField2.Items.Add ("Machine number") CmbField3.Items.Add ("Teacher") CmbField3.Items.Add ("Work Date") CMB FIELD3.ITEMS.ADD ("Work Hours") cmbField3.Items.Add ("Off date") CmbField3.Items.Add ("Off Hours") cmbField3.Items.Add ( "Machine number") End subend class</span>in order to avoid the user not in order to combine the query conditions, the second row and the third row of the query conditions and the second combination of the properties set to be unavailable, only the first composition relationship is not empty when the second row of query conditions and the second combination of talent use, Similarly, the second combination relationship is not empty when the third row combination condition talent is used.
Anyway all this is to reduce our workload, human progress is in fact a "lazy" and constantly explore the process. There are shortcomings in the article also ask the Great God to criticize, hope to be helpful to everyone.
Combined query-How to use window inheritance to achieve less?