机房收费系统之组合查询

来源:互联网 发布:自学电脑编程最快多久 编辑:程序博客网 时间:2024/05/14 15:38

说到组合查询,只能说会者不难,难者不会,只要把思路搞清楚了,知道SQL语句在SQL Server中是如何查找的,那真的就很简单了。

       大家敲的时候可以回想一下我们的学生,当时也算一个组合查询,如果懂了学生,再看看机房是如何敲的,在这里应该注意几个问题

       

        1、  操作符是根据不同的字段名出现的,比如:教师就只有=和<>日期才有< 和>,剩余的选项估计自己就可以分析的出来,

         2、  查询的文本框要防止SQL注入问题,否则就会报错

         3、 最好给出查询日期的输入格式

         4、  起初是只有第一行能用,其他行的combo和text是不能用的,只有组合关系选择“或“或者”与“的时候第二行的才能解锁,同样的道理第二行的组合关系选择“或”或者“与”的时候第三行才能解锁,同时在组合关系的combo控件中添加一个空白的选项,如果用户选了与但是有不想查询了,就可以选择空白

       

         5、数据库查询的内容要适应Mshflexgrid控件,使文本全部使用。

         6、组合查询最好加载的时候有内容,这样用户选择教师的话就不用再次选择了,例如

          

         

          下面是我的代码:

          

AutoColWidth Me, MSHFlexGrid1    Dim mrc As ADODB.Recordset    Dim txtSQL As String    Dim msgtext As String        '如果第一行输入内容有空,提示信息    If Trim(ComboField1.Text) = "" Or Trim(ComOperator1.Text) = "" Or Trim(txtInquire1.Text) = "" Then        MsgBox "请输入完整的查询条件", , "提示"        Exit Sub    End If        '从数据库表worklog_Info表中读取数据    txtSQL = "select * from worklog_Info where  "    'txtSQL = 原来读取的+ 第一个字段+操作符+查询内容    txtSQL = txtSQL & Field(ComboField1.Text) & Trim(ComOperator1.Text) & "'" & Trim(txtInquire1.Text) & "'"        '第一个组合关系不为空    If Trim(Comborelation1.Text <> "") Then            '第二行控件判断消息为空,则提示信息        If Trim(ComboField2.Text) = "" Or Trim(ComOperator2) = "" Or Trim(txtInquire2.Text) = "" Then            MsgBox "您选择了第一个组合关系,请在第二行输入完整条件再查询!", vbOKOnly, "提示"            Exit Sub        Else            '不为空,则从数据库中读取信息。            txtSQL = txtSQL & Field(Comborelation1.Text) & " " & Field(ComboField2.Text) & Trim(ComOperator2.Text) & "'" & Trim(txtInquire2.Text) & "'"                End If    End If        '第二个组合关系不为空    If Trim(Comborelation2.Text) <> "" Then        '条件不完整,提示        If Trim(ComboField3.Text) = "" Or Trim(Comborelation2.Text) = "" Or Trim(txtInquire3.Text) = "" Then            MsgBox "您选择了第二个组合关系,请在第三行输入完整条件再查询!", vbOKOnly, "提示"            Exit Sub        Else            txtSQL = txtSQL & Field(Comborelation2.Text) & " " & Field(ComboField3.Text) & Comborelation2.Text & "'" & Trim(txtInquire3.Text) & "'"        End If    End If'    txtSQL = "select * from line_Info"    Set mrc = ExecuteSQL(txtSQL, msgtext)    With MSHFlexGrid1            .Rows = 1            .CellAlignment = 4            .TextMatrix(0, 0) = "序列号"            .TextMatrix(0, 1) = "教师"            .TextMatrix(0, 2) = "级别"            .TextMatrix(0, 3) = "注册日期"            .TextMatrix(0, 4) = "注册时间"            .TextMatrix(0, 5) = "注销日期"            .TextMatrix(0, 6) = "注销时间"            .TextMatrix(0, 7) = "机器名"            .TextMatrix(0, 8) = "状态"                    Do While Not mrc.EOF                                  .Rows = .Rows + 1           .CellAlignment = 4             .TextMatrix(.Rows - 1, 0) = Trim(mrc.Fields(0))            .TextMatrix(.Rows - 1, 1) = Trim(mrc.Fields(1))            .TextMatrix(.Rows - 1, 2) = Trim(mrc.Fields(2))            .TextMatrix(.Rows - 1, 3) = Trim(mrc.Fields(3))            .TextMatrix(.Rows - 1, 4) = Trim(mrc.Fields(4))            .TextMatrix(.Rows - 1, 5) = Trim(mrc.Fields(5))            .TextMatrix(.Rows - 1, 6) = Trim(mrc.Fields(6))            .TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields(7))             .TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields(8))                       mrc.MoveNext         Loop            mrc.Close    End With    If MSHFlexGrid1.Rows = 1 Then           MsgBox "没有此记录,请重新输入查询信息!", vbOKOnly + vbExclamation, "警告"           Exit Sub       End If

0 0
原创粉丝点击