继上篇博客已过了十天,机房收费系统的博客没有及时更新,接下来几天会陆续“上映”。言归正传,说说组合查询。已经做了“一般查询”,摸索出它们的规律。所以做组合查询之前,我就想“如此雷同,决不纯属巧合”,肯定有窍门的啦!借鉴了别人的一些博客,下边是我整理的内容。机房收费系统中涉及到了4个组合查询(正在上机状态查询、学生基本信息维护、学生上机统计查询、操作员工作记录),它们的过程从高层次上看是相同的,但有些具体步骤的实现不同,这时候,我们把公共的部分放到父窗体中,方便了代码的复用。让具体的实现延迟到子类中。
第一:窗体界面
1、新建组合查询(父窗体)
2、新建子窗体——继承父窗体。
第二:代码
1、父窗体代码
<span style="font-size:14px;">Imports System.Windows.Forms ''' <summary> ''' 组合查询(父窗体) ''' 创建日期:2014-8-21 ''' </summary> ''' <remarks></remarks> Public Class frmGroupQuery Protected GroupQueryEntity As New Entity.GroupQueryEntity ''' <summary> ''' 退出 ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click Me.Close() End Sub ''' <summary> ''' “查询”按钮单击事件 ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Protected Overridable Sub btnQuery_Click(sender As Object, e As EventArgs) Handles btnQuery.Click '使用前先清空控件中的信息 DataGridView.DataSource = Nothing '当第一个组合关系为空的情况 If cmbRelation1.Text = "" Then Dim arrayControl() As Term ReDim Preserve arrayControl(2) arrayControl(0) = New Term(cmbFields1, cmbFields1.Text) '字段 arrayControl(1) = New Term(cmbOperator1, cmbOperator1.Text) '操作符 arrayControl(2) = New Term(txtContext1, txtContext1.Text) '要输入的内容 '调用CheckIsEmpty函数,检查是否为空 If CheckIsEmpty(arrayControl) Then Exit Sub End If End If '当第一个组合关系不为空,第二个为空 If cmbRelation1.Text <> "" And cmbRelation2.Text = "" Then Dim arrayControl() As Term '定义函数 ReDim Preserve arrayControl(5) '函数最小值为6 arrayControl(0) = New Term(cmbFields1, cmbFields1.Text) arrayControl(1) = New Term(cmbOperator1, cmbOperator1.Text) arrayControl(2) = New Term(txtContext1, txtContext1.Text) arrayControl(3) = New Term(cmbFields2, cmbFields2.Text) arrayControl(4) = New Term(cmbOperator2, cmbOperator2.Text) arrayControl(5) = New Term(txtContext2, txtContext2.Text) If CheckIsEmpty(arrayControl) Then Exit Sub End If End If '第二个组合关系不为空 If cmbRelation2.Text <> "" Then Dim arrayControl() As Term ReDim Preserve arrayControl(9) arrayControl(0) = New Term(cmbFields1, cmbFields1.Text) arrayControl(1) = New Term(cmbOperator1, cmbOperator1.Text) arrayControl(2) = New Term(txtContext1, txtContext1.Text) arrayControl(3) = New Term(cmbRelation1, cmbRelation1.Text) arrayControl(4) = New Term(cmbFields2, cmbFields2.Text) arrayControl(5) = New Term(cmbOperator2, cmbOperator2.Text) arrayControl(6) = New Term(txtContext2, txtContext2.Text) arrayControl(7) = New Term(cmbFields3, cmbFields3.Text) arrayControl(8) = New Term(cmbOperator3, cmbFields3.Text) arrayControl(9) = New Term(txtContext3, txtContext3.Text) If CheckIsEmpty(arrayControl) Then Exit Sub End If End If '将参数传递给实体GroupQueryEntity GroupQueryEntity.Fields1 = answer(cmbFields1.Text) GroupQueryEntity.Fields2 = answer(cmbFields2.Text) GroupQueryEntity.Fields3 = answer(cmbFields3.Text) GroupQueryEntity.Operator1 = cmbOperator1.Text GroupQueryEntity.Operator2 = cmbOperator2.Text GroupQueryEntity.Operator3 = cmbOperator3.Text GroupQueryEntity.Context1 = txtContext1.Text.Trim GroupQueryEntity.Context2 = txtContext2.Text.Trim GroupQueryEntity.Context3 = txtContext3.Text.Trim GroupQueryEntity.Relation1 = answer(cmbRelation1.Text) GroupQueryEntity.Relation2 = answer(cmbRelation2.Text) GroupQueryEntity.Table = GetTable() '表名。通过调用一个函数来获取表名 '当cmbRelation1为空时, If cmbRelation1.Text = "" Then GroupQueryEntity.Fields2 = "1" GroupQueryEntity.Operator2 = "=" GroupQueryEntity.Context2 = "1" GroupQueryEntity.Fields3 = "1" GroupQueryEntity.Operator3 = "=" GroupQueryEntity.Context3 = "1" GroupQueryEntity.Relation1 = "与" GroupQueryEntity.Relation2 = "与" End If If cmbRelation2.Text = "" Then GroupQueryEntity.Relation2 = "与" GroupQueryEntity.Fields3 = "1" GroupQueryEntity.Operator3 = "=" GroupQueryEntity.Context3 = "1" End If '调用子类的查询,返回不同的查询结果 buttnQuery() End Sub ''' <summary> ''' 定义虚函数answer,根据选择条件的不同转换成不同的数据库字段 ''' </summary> ''' <param name="GetResult">参数,根据comboBox控件内容的不同获取不同结果</param> ''' <returns>返回GetResult,根据选择调解的不同转换成不同的数据库字段</returns> ''' <remarks>刘星 2014-8-22</remarks> Protected Overridable Function answer(ByVal GetResult As String) As String Return "" '返回值 End Function ''' <summary> ''' 定义虚函数GetTable,获取不同数据库的表名 ''' </summary> ''' <returns>返回获取的不同的表或视图</returns> ''' <remarks>刘星 2014-8-22</remarks> Protected Overridable Function GetTable() As String Return "" End Function ''' <summary> ''' 窗体加载,赋初值 ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks>刘星 2014-8-22</remarks> Private Sub frmGroupQuery_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim items As String() = {"=", "<", ">", "<>"} '操作符内容 Dim combs As ComboBox() = {cmbOperator1, cmbOperator2, cmbOperator3} '定义comboBox控件,包含三种combo '制定循环,遍历combs这个组 For Each c As ComboBox In combs c.Items.AddRange(items) '像控件combos的每个控件的项列表添加项的数组 Next cmbRelation1.Items.Add("与") cmbRelation1.Items.Add("或") cmbRelation2.Items.Add("与") cmbRelation2.Items.Add("或") End Sub ''' <summary> ''' 连接字符串 ''' </summary> ''' <param name="GroupQueryEntity"></param> ''' <returns></returns> ''' <remarks></remarks> Protected Function ConnectionStr(ByVal GroupQueryEntity As Entity.GroupQueryEntity) As String ConnectionStr = GroupQueryEntity.Fields1 & GroupQueryEntity.Operator1 & "'" & GroupQueryEntity.Context1 & "'" & " " & answer(GroupQueryEntity.Relation1) & " " & GroupQueryEntity.Fields2 & "" & GroupQueryEntity.Operator2 & "'" & GroupQueryEntity.Context2 & "'" & " " & answer(GroupQueryEntity.Relation2) & " " & GroupQueryEntity.Fields3 & "" & GroupQueryEntity.Operator3 & "'" & GroupQueryEntity.Context3 & "'" Return ConnectionStr End Function ''' <summary> ''' 子窗体的查询继承此方法过程 ''' </summary> ''' <remarks></remarks> Protected Overridable Sub buttnQuery() End Sub ''' <summary> ''' 导出到Excel ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub btnExcel_Click(sender As Object, e As EventArgs) Handles btnExcel.Click Module1.ExportExcel1.ExportExcel(DataGridView) End Sub End Class</span>
2、子窗体U层代码
<span style="font-size:14px;">Imports System.Windows.Forms ''' <summary> ''' 组合查询——学生基本信息维护 ''' </summary> ''' <remarks></remarks> Public Class frmGroupStuInfo ''' <summary> ''' 学生基本信息维护。组合框加载内容。窗体名称。 ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub frmGroupStuInfo_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim items As String() = {"卡号", "学号", "姓名", "性别", "系别", "年级", "班级"} Dim combs As ComboBox() = {cmbFields1, cmbFields2, cmbFields3} For Each c As ComboBox In combs c.Items.AddRange(items) Next Me.Text = "学生基本信息维护" End Sub ''' <summary> ''' 重写虚函数GetTable,选择数据表 ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Protected Overrides Function GetTable() As String GroupQueryEntity.Table = "T_Student" Return GroupQueryEntity.Table End Function ''' <summary> ''' 重写父类函数answer,将U层文字转换成数据库识别的字段 ''' </summary> ''' <param name="GetResult"></param> ''' <returns></returns> ''' <remarks></remarks> Protected Overrides Function answer(GetResult As String) As String Select Case GetResult Case "卡号" Return "stuCard" Case "学号" Return "stuID" Case "姓名" Return "stuName" Case "金额" Return "stuCash" Case "性别" Return "stuSex" Case "系别" Return "stuDepart" Case "年级" Return "stuGrade" Case "班级" Return "stuClass" Case "与" Return "and" Case "或" Return "or" Case Else Return "" End Select Return MyBase.answer(GetResult) End Function ''' <summary> ''' 学生基本信息查询——子窗体的“查询” ''' </summary> ''' <remarks></remarks> Protected Overrides Sub buttnQuery() MyBase.buttnQuery() DataGridView.Columns.Add("stuID", "学号") DataGridView.Columns.Add("stuName", "姓名") DataGridView.Columns.Add("stuCard", "卡号") DataGridView.Columns.Add("stuCash", "金额") DataGridView.Columns.Add("stuDepart", "系别") DataGridView.Columns.Add("stuGrade", "年级") DataGridView.Columns.Add("stuClass", "班级") DataGridView.Columns.Add("stuSex", "性别") DataGridView.Columns.Add("stuStatus", "状态") DataGridView.Columns.Add("stuNote", "备注") '绑定数据库列的名称 DataGridView.Columns(0).DataPropertyName = "stuID" DataGridView.Columns(1).DataPropertyName = "stuName" DataGridView.Columns(2).DataPropertyName = "stuCard" DataGridView.Columns(3).DataPropertyName = "stuCash" DataGridView.Columns(4).DataPropertyName = "stuDepart" DataGridView.Columns(5).DataPropertyName = "stuGrade" DataGridView.Columns(6).DataPropertyName = "stuClass" DataGridView.Columns(7).DataPropertyName = "stuSex" DataGridView.Columns(8).DataPropertyName = "stuStatus" DataGridView.Columns(9).DataPropertyName = "stuNote" DataGridView.AutoGenerateColumns = False '不允许自动创建列 Dim FcGroupStuInfo As New Facade.FcGroupQuery Dim sqlStr As String sqlStr = ConnectionStr(GroupQueryEntity) DataGridView.DataSource = FcGroupStuInfo.GroupStuInfo(GroupQueryEntity, sqlStr) End Sub End Class </span>
好了,剩下的B层D层按部就班的调用就好。其中父窗体中有一段字符串连接的代码,自己在SqlServer中新建查询举个例子就能明白了。例如“select * from T_Student where cardID=1”和“select * from T_Student where cardID=1 and 1=1” 实践一下就知道啦!
可能里边也有不合理的代码,希望大家指正,不断完善,也希望对大家有些帮助!