三个组合框,每个长的都一样,就是标题呀,查询内容不一样,很多相似的地方。根据师哥师姐在讲设计模式的时候,讲到了模版方法提到了组合查询,等到了自己该做组合查询的时候就有了这么一个方向。用模板方法。因为师姐当时讲的已经很详细了,所以关于模板具体内容就不在赘述。
关于建一个继承窗体大家请参照师姐的博客:http://blog.csdn.net/liutengteng130/article/details/8869685
一下是组合查询的Uml图:
代码的实现:
父类代码:
Private Sub frmSCStatusInquiry_Load(sender As Object, e As EventArgs) Handles MyBase.Load '将每个表共同的操作符号提取出来 '每个窗体的控件的名称都是一样的 ' '同时cmbOprational ,cmbCombination 这两个控件中德值都是一样的,所以在父类窗体中进行加载 '因为三个控件加载的动西都是一样的 '写一个便利循环, '首先定义一个字符串,分别储存这样的一组字符 Dim items As String() = {" ", "=", "<", ">", "<>"} '定义一个comboBox控件 其中包含了(界面中德三个控件) Dim combs As ComboBox() = {cmbOprational1, cmbOprational2, cmbOprational3} 'in子句,制定循环,要在forEach循环中遍历的组,便利循环combs 这个组 For Each c As ComboBox In combs '想控件cmbos的每一个控件的项列表添加项的数组 c.Items.AddRange(items) Next cmbCombination1.Items.Add("") cmbCombination1.Items.Add("与") cmbCombination1.Items.Add("或") cmbCombination2.Items.Add("") cmbCombination2.Items.Add("与") cmbCombination2.Items.Add("或") ' 给cmbWord!付给一个初始值 1 = 1 cmbWord1.Text = "1" cmbWord2.Text = "1" cmbWord3.Text = "1" '给require.Table 中传入一个值,这个值决定者在外观中选择哪一个方法 require.table = Table() end sub
<pre name="code" class="vb"> Private Sub butExit_Click(sender As Object, e As EventArgs) Handles butExit.Click Me.Close() End Sub '父类中的这个ButInquire中写入各个子类都相同的代码 Protected Overridable Sub butInquiry_Click(sender As Object, e As EventArgs) Handles butInquiry.Click '判断内容是否唯恐 '判断第一组下拉菜单不能为空 If cmbCombination1.Text = "" Then Dim arrayControl() As Term ReDim Preserve arrayControl(2) arrayControl(0) = New Term(cmbWord1, cmbWord1.Text) arrayControl(1) = New Term(cmbOprational1, cmbOprational1.Text) arrayControl(2) = New Term(txtInquiry1, txtInquiry1.Text) If IsSomeEmptyText(arrayControl) Then Exit Sub End If End If '如果第一组的那个关系又,所以就要判断下一组是否为空值 If cmbCombination1.Text <> "" Then Dim arrayControl() As Term ReDim Preserve arrayControl(5) arrayControl(0) = New Term(cmbWord1, cmbWord1.Text) arrayControl(1) = New Term(cmbOprational1, cmbOprational1.Text) arrayControl(2) = New Term(txtInquiry1, txtInquiry1.Text) arrayControl(3) = New Term(cmbWord2, cmbWord2.Text) arrayControl(4) = New Term(cmbOprational2, cmbCombination2.Text) arrayControl(5) = New Term(txtInquiry2, txtInquiry2.Text) If IsSomeEmptyText(arrayControl) Then Exit Sub End If End If '如果第二个关系也不为空值 '换一种方法要判断是不是全部为空值 '这个方法可以在模块中在写一个查询是否全部为空值,这样就省事了 If cmbCombination2.Text <> "" Then Dim arrayControl() As Term ReDim Preserve arrayControl(9) arrayControl(0) = New Term(cmbWord1, cmbWord1.Text) arrayControl(1) = New Term(cmbOprational1, cmbOprational1.Text) arrayControl(2) = New Term(txtInquiry1, txtInquiry1.Text) arrayControl(3) = New Term(cmbCombination1, cmbCombination1.Text) arrayControl(4) = New Term(cmbWord2, cmbWord2.Text) arrayControl(5) = New Term(cmbOprational2, cmbOprational2.Text) arrayControl(6) = New Term(txtInquiry2, txtInquiry2.Text) arrayControl(7) = New Term(cmbWord3, cmbWord3.Text) arrayControl(8) = New Term(cmbOprational3, cmbOprational3.Text) arrayControl(9) = New Term(txtInquiry3, txtInquiry3.Text) If IsSomeEmptyText(arrayControl) Then Exit Sub End If End If '将控件中德值,传入查询这个实体中,这样更好的做好了D曾和U层的解耦合 '因为实体的存在,可以让这些层之间更好的独立 require.cmbWord1 = answer(cmbWord1.Text) require.cmbWord2 = answer(cmbWord2.Text) require.cmbWord3 = answer(cmbWord3.Text) require.cmbOprational1 = cmbOprational1.Text require.cmbOprational2 = cmbOprational2.Text require.cmbOprational3 = cmbOprational3.Text require.txtInquiry1 = txtInquiry1.Text require.txtInquiry2 = txtInquiry2.Text require.txtInquiry3 = txtInquiry3.Text require.cmbCombination1 = answer(cmbCombination1.Text) require.cmbCombination2 = answer(cmbCombination2.Text) '在查询的时候因为查询语句中德“1=1” '所以可以设置一个默认值 '下边就是当cmbCombination为空时侯,后边的一些值设置为默认值 If cmbCombination1.Text = "" Then require.cmbWord2 = "1" require.cmbOprational2 = "=" require.txtInquiry2 = "1" require.cmbOprational3 = "=" require.cmbWord3 = "1" require.txtInquiry3 = "1" require.cmbCombination1 = "与" require.cmbCombination2 = "与" End If '当cmbCombination2为空的时候,将其后边的设置默认值 If cmbCombination2.Text = "" Then require.cmbCombination2 = "与" require.cmbWord3 = "1" require.cmbOprational3 = "=" require.txtInquiry3 = "1" End If '在子类中变化的就是这个方法,导致查询的结果是不一样的 combinClick() End Sub '讲中文变为英文的返回值 Protected Overridable Function answer(ByVal getResult As String) As String Return "" '返回值 End Function '设置这个方法,是为了给查询实体的Table属性,赋值 '这样以便更好的去外观中招到相应的值 Protected Overridable Function Table() As String Return "" End Function
Protected Function connectionStr(ByVal require As Entity.RequireEntity) As String connectionStr = answer(require.cmbWord1) & require.cmbOprational1 & "'" & require.txtInquiry1 & "'" & " " & answer(require.cmbCombination1) & " " & answer(require.cmbWord2) & "" & require.cmbOprational2 & "'" & require.txtInquiry2 & "'" & " " & answer(require.cmbCombination2) & " " & answer(require.cmbWord3) & "" & require.cmbOprational3 & "'" & require.txtInquiry3 & "'" Return connectionStr end Function
子类代码
Public Class frmStuOnlineInfo Private Sub frmStuOnlineInfo_Load(sender As Object, e As EventArgs) Handles MyBase.Load '因为三个控件加载的动西都是一样的 '写一个便利循环, '首先定义一个字符串,分别储存这样的一组字符 Dim items As String() = {"卡号", "学号", "上机时间", "下机时间", "消费时间", "消费金额", "用户类型"} '定义一个comboBox控件 其中包含了(界面中德三个控件) Dim combs As ComboBox() = {cmbWord1, cmbWord2, cmbWord3} 'in子句,制定循环,要在forEach循环中遍历的组,便利循环combs 这个组 For Each c As ComboBox In combs '想控件cmbos的每一个控件的项列表添加项的数组 c.Items.AddRange(items) Next End Sub '重写父类中的answer这个方法 Protected Overrides Function answer(getResult As String) As String Select Case getResult Case "卡号" Return "cardId" Case "学号" Return "stuId" Case "上机时间" Return "startTime" Case "下机时间" Return "endTime" Case "消费时间" Return "spentTime" Case "消费金额" Return "consumption" Case "用户类型" Return "type" Case "与" Return "and" Case "或" Return "or" Case Else Return getResult End Select End Function '重写父类中的表这个方法 Protected Overrides Function Table() As String require.table = "T_CashsRecord" Return require.table End Function Protected Overrides Sub combinClick() '先设置dataGridView 的属性,添加列,再编辑列 DataGridView1.Columns.Add("cardId", "卡号") DataGridView1.Columns.Add("stuId", "学号") DataGridView1.Columns.Add("startTime", "上机时间") DataGridView1.Columns.Add("endTime", "下机时间") DataGridView1.Columns.Add("spentTime", "消费时间") DataGridView1.Columns.Add("consumption", "消费金额") DataGridView1.Columns.Add("type", "用户类型") DataGridView1.Columns(0).DataPropertyName = "cardId" DataGridView1.Columns(1).DataPropertyName = "stuId" DataGridView1.Columns(2).DataPropertyName = "startTime" DataGridView1.Columns(3).DataPropertyName = "endTime" DataGridView1.Columns(4).DataPropertyName = "spentTime" DataGridView1.Columns(5).DataPropertyName = "consumption" DataGridView1.Columns(6).DataPropertyName = "type" DataGridView1.AutoGenerateColumns = False '定义一个字符串 '将connectionStr中所返回的字符串给str Dim str As String str = connectionStr(require) Dim onlie As IList(Of Entity.CardConsume) Dim mgr As New BLL.SStatiusRecordFace '注意(of entity.Cardconsume )在写这个combin的抽象的方法的时候,其中申明过一个(OfT)这里的格式要和哪里的一样 onlie = mgr.combin(Of Entity.CardConsume)(require, str) DataGridView1.DataSource = onlie End Sub End Class
以这种方法,可以省去很多的代码量。模板方法最初在网上找了很多的资料,虽然有的是借鉴的,但是其中一些小的细节中也加入了自己的想法。比如说,查询的字符串,要判断组合条件是否唯恐,但是根据“1=1.”的原理,这里就写了一条语句就可以了,这还需要大家打数据库中去验证一下。实践出真知。
想起了米老师的一句话,先把复杂变得简单,在简单完成后再抽象出规律来。这样一个过程你才会这道这个规律的好处。
脚踏实地,勿想一步登天!