机房收费系统——组合查询

机房收费系统——组合查询机房收费系统——组合查询

大家好,又见面了,我是你们的朋友全栈君。组合查询也是我们在机房收费系统中接触到的一个新东西,一开始无从下手,后来也在摸爬滚打中实现了。首先思路要清晰,明白这些功能的内在逻辑关系,代码实现起来才会更顺畅。

就拿操作员工作记录窗体来说,
机房收费系统——组合查询

 一.判断第1行查询条件是否为空,若都不为空而且组合关系4 为空,查询到的结果符合第一行查询条件。

二.若组合关系4 为“与” 或 “或”,且组合关系5 为空,则第二行查询条件都不能为空,查询到的结果符合第一行和       第二行查询条件组合后查询到的内容。
三.若组合关系4和5都不为空,三行查询条件都得填好,则查询到的是三行共同组合后的内容。

如果4为空,则2,3,5中控件的enabled属性为False,如果不为空,将激活2,5中控件的enabled属性为True。
’现在发现自己敲的时候没注意明明规范,大家可能看不太懂
'combo1:字段名
'combo2:操作符
'combo3:组合关系


Private Sub Form_Load()
    Dim i, j, k
    Me.Height = 10185
    Me.Width = 15135

    '加载窗体时依次填充comboBox的下拉列表
    For i = 0 To 2
        With Combo1(i)
            .AddItem "教师"
            .AddItem "注册时间"
            .AddItem "注册日期"
            .AddItem "注销时间"
            .AddItem "注销日期"
            .AddItem "机器名"
        End With
    Next i
    
    For j = 0 To 2
        With Combo2(j)
            .AddItem "="
            .AddItem "<"
            .AddItem ">"
            .AddItem "<>"
        End With
    Next j
    
    For k = 0 To 1
        With Combo3(k)
            .AddItem "与"
            .AddItem "或"
        End With
    Next k

  ’如果组合查询框为空(不是“与”也不是“或”),不能添加下面的查询条件
   If Combo3(0).Text = Trim("") Then
        Combo1(1).Enabled = False
        Combo1(2).Enabled = False
        Combo2(1).Enabled = False
        Combo2(2).Enabled = False
        Text2.Enabled = False
        Text3.Enabled = False
        Combo3(1).Enabled = False
    End If
    
    If Combo3(1).Text = Trim("") Then
        Combo1(2).Enabled = False
        Combo2(2).Enabled = False
        Text3.Enabled = False
    End If  
End Sub


'激活
Private Sub Combo3_Click(Index As Integer)
    If Combo3(0).Text <> Trim("") Then
        Combo1(1).Enabled = True
        Combo2(1).Enabled = True
        Combo3(1).Enabled = True
        Text2.Enabled = True
    End If
    
    If Combo3(1).Text <> Trim("") Then
        Combo1(2).Enabled = True
        Combo2(2).Enabled = True
        Text3.Enabled = True
    End If
    
End Sub


Private Sub cmdInquire_Click()
    Dim MsgText As String
    Dim txtSQL As String
    Dim mrc As ADODB.Recordset
    Dim strCon1, strCon2, strcon3, strcon4, strcon5
  
’把汉字转换成数据库中的字段名,实现第一行查询  
    Select Case Combo1(0).Text
        Case "教师"
            strCon1 = " UserID"
        Case "注册时间"
            strCon1 = " LoginTime"
        Case "注册日期"
            strCon1 = " LoginDate"
        Case "注销时间"
            strCon1 = "LogoutTime"
        Case "注销日期"
            strCon1 = "logoutDate"
        Case "机器名"
            strCon1 = " Computer"
    End Select
    
    If Not Testtxt(Combo1(0).Text) Then
        MsgBox "请选择字段名", vbOKOnly + vbExclamation, "提示"
        Combo1(0).SetFocus
        Exit Sub
    ElseIf Not Testtxt(Combo2(0).Text) Then
        MsgBox "请选择操作符", vbOKOnly + vbExclamation, "提示"
        Combo2(0).SetFocus
        Exit Sub
    ElseIf Not Testtxt(Text1.Text) Then
        MsgBox "请输入要查询的内容"
        Text1.SetFocus
        Exit Sub
    Else
        txtSQL = "select * from worklog_Info where " & strCon1 & Combo2(0).Text & "'" & Text1.Text & "'"
        Set mrc = ExecuteSQL(txtSQL, MsgText)
        
        If mrc.EOF Then
            MsgBox "该条件的数据不存在", vbOKOnly + vbExclamation, "提示"
            Exit Sub
        End If
    End If
 
'实现第二行查询   
    If Not Testtxt(Combo3(0).Text) Then
        GoTo Case1
    Else
        Select Case Combo3(0).Text
            Case "与"
                strCon2 = "and"
            Case "或"
                strCon2 = "or"
        End Select
                
        Select Case Combo1(1).Text
            Case "教师"
                strcon3 = "UserID"
            Case "注册时间"
                strcon3 = "LoginTime"
            Case "注册日期"
                strcon3 = "LoginDate"
            Case "注销时间"
                strcon3 = " LogoutTime"
            Case "注销日期"
                strcon3 = " logoutDate"
            Case "机器名"
                strcon3 = "Computer"
        End Select
        
        If Not Testtxt(Combo1(1).Text) Then
            MsgBox "请选择字段名", vbOKOnly + vbExclamation, "提示"
            Combo1(1).SetFocus
            Exit Sub
        ElseIf Not Testtxt(Combo2(1).Text) Then
            MsgBox "请选择操作符", vbOKOnly + vbExclamation, "提示"
            Combo2(1).SetFocus
            Exit Sub
        ElseIf Not Testtxt(Text2.Text) Then
            MsgBox "请输入要查询的内容", vbOKOnly + vbExclamation, "提示"
            Text2.SetFocus
            Exit Sub
        ElseIf Combo1(1).ListIndex = 1 Or 2 Or 3 Or 4 Then
            MonthView1.Visible = True
            Text2.Text = MonthView1.Value
        Else
            txtSQL = txtSQL & strCon2 & " " & strcon3 & Combo2(1).Text & "'" & Text2.Text & "'"
            Set mrc = ExecuteSQL(txtSQL, MsgText)
            
            If mrc.EOF Then
                MsgBox "该条件的数据不存在", vbOKOnly + vbExclamation, "提示"
                Exit Sub
            End If
        End If
    End If
    
’实现第三行查询
    If Trim(Combo3(1).Text) = "" Then
        GoTo Case1
    Else
        Select Case Combo3(1).Text
            Case "与"
                strcon4 = "and"
            Case "或"
                strcon4 = "or"
        End Select
        
        Select Case Combo1(2).Text
            Case "教师"
                strcon5 = "UserID"
            Case "注册时间"
                strcon5 = "LoginTime"
            Case "注册日期"
                strcon5 = "LoginDate"
            Case "注销时间"
                strcon5 = " LogoutTime"
            Case "注销日期"
                strcon5 = " logoutDate"
            Case "机器名"
                strcon5 = "Computer"
        End Select
    

        If Not Testtxt(Combo1(2).Text) Then
            MsgBox "请选择字段名", vbOKOnly + vbExclamation, "提示"
            Combo1(2).SetFocus
            Exit Sub
        End If
        
        If Not Testtxt(Combo2(2).Text) Then
            MsgBox "请选择操作符", vbOKOnly + vbExclamation, "提示"
            Combo2(2).SetFocus
            Exit Sub
        End If
        
        If Not Testtxt(Text3.Text) Then
            MsgBox "请输入要查询的内容", vbOKOnly + vbExclamation, "提示"
            Text3.SetFocus
            Exit Sub
        End If
        
        If Combo1(2).ListIndex = 1 Or 2 Or 3 Or 4 Then
            MonthView1.Visible = True
            Text3.Text = MonthView1.Value
        End If
        
        txtSQL = txtSQL & strcon4 & " " & strcon5 & Combo2(2) & "'" & Text3.Text & "'"
        Set mrc = ExecuteSQL(txtSQL, MsgText)
        
        If mrc.EOF Then
            MsgBox "该条件的数据不存在", vbOKOnly + vbExclamation, "提示"
            Exit Sub
        End If
    End If
        
    
Case1:
    With myflexgrid
        .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) = Format(mrc.Fields(5))
            .TextMatrix(.Rows - 1, 6) = Format(mrc.Fields(6))
            .TextMatrix(.Rows - 1, 7) = Trim(mrc.Fields(7))
            .TextMatrix(.Rows - 1, 8) = Trim(mrc.Fields(8))
            mrc.MoveNext
        Loop
    End With  
End Sub

这么长的代码终于实现了组合查询的功能,但是大家可能发现在查询的过程中,有很多代码是重复的,有没有更好的方法呢?
优化1:
在代码中多次用到了把comboBox中下拉列表的汉字转换成连接数据库时可以识别的,与数据库字段一致的英文,这些代码多次重复,是不是会占用很多内存呢?我们可以自定义一个函数,解决这个问题。
Private Function FieldName(strFieldName as String) As String
        Select Case strFieldName
            Case "教师"
                FieldName = "UserID"
            Case "注册时间"
                FieldName = "LoginTime"
            Case "注册日期"
                FieldName = "LoginDate"
            Case "注销时间"
                FieldName = " LogoutTime"
            Case "注销日期"
                FieldName = " logoutDate"
            Case "机器名"
                FieldName = "Computer"
        End Select

End Sub

'以条件1和2组合为例,相应的连接数据库的代码可改为

Private Sub cmdInquire_Click()

	txtSQL="select * from worklog_Info where " & FieldName(combo1(0).Text) 
	       & combo2(0).Text & "'" & text1.Text & "'"
	Select Case combo3(0).Text
	     Case "或"
		txtSQL=txtSQL & "or" & FieldName(combo1(1).Text) 
			& combo2(1).Text & "'" & text2.Text & "'"
	     Case "与"
		txtSQL=txtSQL & "and" & FieldName(combo1(1).Text) 
			& combo2(1).Text & "'" & text2.Text & "'"
	End Select
End Sub
Private Function FieldName1(strFieldName as String)As String
	Select Case strFieldName
	      Case "与"
		  FieldName = "and"
	      Case "或"
		  FieldName = "or"
	End Select
End Sub
'或者也可以这么写
Private Sub cmdInquire_Click()
	txtSQL="select * from worklog_Info where "
	strA= FieldName(combo1(0).Text) & combo2(0).Text & "'" & text1.Text & "'"
	strB=FieldName1(combo3(0).Text) & FieldName(combo1(1).Text) & combo2(1).Text 
		& "'" & text2.Text & "'"
	strC=FieldName1(combo3(1).Text) & FieldName(combo1(2).Text) & combo2(2).Text 
		& "'" & text3.Text & "'"
	txtSQL=txtSQL & strA & strB & strC
End Sub		

优化2:
在最初的代码中,也曾多次用到了判断查询条件是否为空的语句,看师哥师姐的博客,也可以用一个相应的函数来实现
Public Function Testtxt(txt As String) As Boolean
    If Trim(txt) = "" Then
        Testtxt = False
    Else
        Testtxt = True
    End If
End Function
Private Sub cmdInquire_Click()
    isEmpty= Testtxt(combo1(0).Text) or Testtxt(combo2(0).Text) or Testtxt(text1.Text)
    
    If isEmpty=False Then
	Msgbox"请输入查询条件",48,"提示"
    End If
End Sub

用这种方法使代码精简了不少,但是与最初的相比,用户不能直接明了的看出具体是哪个查询条件没选,各有各的好处。
组合查询也还是基本的查询,就是在写与数据库连接的语句时遇到了一些问题,细心一些就好了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/105791.html原文链接:https://javaforall.net

(0)
上一篇 2022年4月24日 下午8:20
下一篇 2022年4月24日 下午8:20


相关推荐

  • MySQL:MySQL 存储过程

    MySQL:MySQL 存储过程MySQL存储过程(了解)1什么是存储过程MySQL5.0版本开始支持存储过程存储过程(StoredProcedure)是一种在数据库中存储复杂程序,以便外部程序调用的一种数据库对象

    2022年7月3日
    24
  • 特征金字塔网络 FPN

    特征金字塔网络 FPN一 提出背景 nbsp nbsp nbsp nbsp 论文 FeaturePyram nbsp 点击下载 nbsp nbsp nbsp nbsp 在传统的图像处理方法中 金字塔是比较常用的一种手段 像 SIFT 基于金字塔做了多层的特征采集 对于深度网络来讲 其原生的卷积网络特征决定了天然的金字塔结构 深度网络在目标检测领域的应用比如早期的 FastRCNN FasterRCNN 都

    2026年3月18日
    2
  • shell basename的简单用法

    shell basename的简单用法basename 去掉文件名的路径和后缀 habsenamepat gt 如果指定了 suffix basename 就会吧路径和 suffix 都去掉 habsenamepat gt 如果指定了 suffix basename 就会吧路径和 suffix 都去掉 eg basename tmp test file txt 只指定了文件名 file t

    2026年3月18日
    2
  • 远程mysql报错GHost ‘xxx‘ is not allowed to connect to this MySQL serverConnection closed by foreign host

    远程mysql报错GHost ‘xxx‘ is not allowed to connect to this MySQL serverConnection closed by foreign host异常在另一个主机执行 telnet 命令远程访问 mysql 的 3306 端口报错 GHost 192 168 88 132 isnotallowed 原因 mysql 数据库不允许远程主机访问当前数据库 解决修改 user 表的 host 项 在 192 168 88 133 号主机中登录 mysql 然后执行如下命令 选择 mysql 数据库 usemysql

    2026年3月26日
    2
  • WinRunner介绍

    WinRunner介绍WinRunner 介绍 不断更新 nbsp nbsp nbsp nbsp nbsp WinRunner 在项目中的作用可以用 WinRunner 为所测试应用程序的 GUI 功能和回归测试创建自动化脚本 nbsp nbsp nbsp nbsp nbsp nbsp nbsp WinRunner 的测试过程主要包括如下 6 个阶段 1 nbsp nbsp nbsp 创建 GUIMap 文件 WinRunner 可以通过它来识别被测试应用程

    2026年3月19日
    2
  • String.Format使用方法

    String.Format使用方法

    2021年11月23日
    55

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号