二见钟情之组合查询(vb.net版)

第一遍做机房系统组合查询的时候用的是VB6.0,当时就一个感觉:乱。当时做的时候稀里糊涂的,好不容易应付了事,松了口气。这次是第二遍,用VB.NET还没开始做呢,心里就开始打鼓了,不过做完了以后,发现还是挺有意思的。在做之前也是一边各种查一边各种想,我用的是拼接字符串,主要是参考了高迎的博客。下面以“学生基本信息维护”为例来说说我是怎么做的。

首先是界面:

二见钟情之组合查询(vb.net版)_第1张图片

首先是U层代码,这一层的代码觉得还是很重要的

Imports BLL
Imports Entity

Public Class frmStudentData


    Private Sub frmStudentData_Load(sender As Object, e As EventArgs) Handles Me.Load

        '窗体刚加载的时候,只允许第一组查询条件可用
        comboField2.Enabled = False
        comboOperate2.Enabled = False
        comboCheck2.Enabled = False
        comboUnion2.Enabled = False
        comboField3.Enabled = False
        comboOperate3.Enabled = False
        comboCheck3.Enabled = False

        comboUnion1.Items.Clear()
        comboUnion1.Items.Add("")
        comboUnion1.Items.Add("或")
        comboUnion1.Items.Add("与")


    End Sub


    Private Sub comboUnion1_TextChanged(sender As Object, e As EventArgs) Handles comboUnion1.TextChanged
        '当鼠标点击第一个组合关系框的时候,判断第一组查询条件是否填写完整
        '若不完整,给出提示
        '若完整,则第二组查询条件和第二个关系组合框变为可用
        If comboField1.Text <> "" And comboOperate1.Text <> "" And comboCheck1.Text <> "" Then
            comboField2.Enabled = True
            comboOperate2.Enabled = True
            comboCheck2.Enabled = True
            comboUnion2.Enabled = True

            comboUnion2.Items.Clear()
            comboUnion2.Items.Add("或")
            comboUnion2.Items.Add("与")

        Else
            MsgBox("请将第一组查询条件填写完整!", , "提示")
            Exit Sub
        End If

    End Sub

    Private Sub comboUnion2_TextChanged(sender As Object, e As EventArgs) Handles comboUnion2.TextChanged
        '当鼠标点击第二个组合关系框的时候,判断第二组查询条件是否填写完整
        '若不完整,给出提示
        '若完整,则第三组查询条件可用
        If comboField2.Text <> "" And comboOperate2.Text <> "" And comboCheck2.Text <> "" Then
            comboField3.Enabled = True
            comboOperate3.Enabled = True
            comboCheck3.Enabled = True
        Else
            MsgBox("请将第二组查询条件填写完整", , "提示")
            Exit Sub
        End If
    End Sub

    Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
        End
    End Sub

    Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
        If comboField1.Text = "" Or comboOperate1.Text = "" Or comboCheck1.Text = "" Then
            MsgBox("请将查询条件填写完整!", , "提示")
            Exit Sub
        End If



        '拼接字符串
        Dim sqlstring As String = Nothing

        '按照组合框的组合关系来分别针对每一种组合关系来拼接字符串
        '当组合关系框均为空时
        If comboUnion1.Text = "" Then
            sqlstring = GetEnglish(comboField1.Text) + comboOperate1.Text + comboCheck1.Text
            '当第一个组合框不为空,第二个组合框为空时有两种情况:
            '1、“或”“空”
        ElseIf comboUnion1.Text = "或" And comboUnion2.Text = "" Then
            sqlstring = GetEnglish(comboField1.Text) + comboOperate1.Text + comboCheck1.Text Or GetEnglish(comboField2.Text) + comboOperate2.Text + comboCheck2.Text
            '2、“与”“空”
        ElseIf comboUnion1.Text = "与" And comboUnion2.Text = "" Then
            sqlstring = GetEnglish(comboField1.Text) + comboOperate1.Text + comboCheck1.Text And GetEnglish(comboField2.Text) + comboOperate2.Text + comboCheck2.Text
            
		 '当两个组合框均不为空时,有四种组合情况
            '1、“或”“或”
        ElseIf comboUnion1.Text = "或" And comboUnion2.Text = "或" Then
            sqlstring = GetEnglish(comboField1.Text) + comboOperate1.Text + comboCheck1.Text Or GetEnglish(comboField2.Text) + comboOperate2.Text + comboCheck2.Text Or GetEnglish(comboField3.Text) + comboOperate3.Text + comboCheck3.Text


            '2、“与”“与”
        ElseIf comboUnion1.Text = "与" And comboUnion2.Text = "与" Then
            sqlstring = GetEnglish(comboField1.Text) + comboOperate1.Text + comboCheck1.Text And GetEnglish(comboField2.Text) + comboOperate2.Text + comboCheck2.Text And GetEnglish(comboField3.Text) + comboOperate3.Text + comboCheck3.Text

            '3、“或”“与”
        ElseIf comboUnion1.Text = "或" And comboUnion2.Text = "与" Then
            sqlstring = GetEnglish(comboField1.Text) + comboOperate1.Text + comboCheck1.Text Or GetEnglish(comboField2.Text) + comboOperate2.Text + comboCheck2.Text And GetEnglish(comboField3.Text) + comboOperate3.Text + comboCheck3.Text

            '4、“与”“或”
        ElseIf comboUnion1.Text = "与" And comboUnion2.Text = "或" Then
            sqlstring = GetEnglish(comboField1.Text) + comboOperate1.Text + comboCheck1.Text And GetEnglish(comboField2.Text) + comboOperate2.Text + comboCheck2.Text Or GetEnglish(comboField3.Text) + comboOperate3.Text + comboCheck3.Text

        End If

        '调用B层函数
        Dim dtStuData As DataTable = StuCardManageBLL.CheckStuData(sqlstring)
        If dtStuData.Rows.Count = 0 Then
            MsgBox("没有记录,请重试!", , "提示")

            Exit Sub
            dtStuData.Clear()
            DataGridView1.DataSource = Nothing
            DataGridView1.Refresh()

        Else
            '显示记录
            DataGridView1.Refresh()
            DataGridView1.DataSource = dtStuData

        End If

    End Sub


    '将界面上的中文字段名转换为数据库中的英文字段名
    Public Function GetEnglish(ByVal strField As String) As String
        Select Case (strField)
            Case "卡号"
                Return "CardNo"
            Case "学号"
                Return "StudentNo"
            Case "姓名"
                Return "Name"
            Case "性别"
                Return "Sex"
            Case "系别"
                Return "Department"
            Case "年级"
                Return "Grade"
            Case "班级"
                Return "Class"
            Case "教师"
                Return "Teacher"
            Case Else
                Return ""
        End Select
    End Function


B层代码:

   '组合查询
 '学生基本信息维护
 Public Shared Function CheckStuData(ByVal sqlstring As String) As DataTable
 '调用D层函数
 Dim dtStuData As DataTable = sqlStuCardManage.CheckStuData(sqlstring)
 Return dtStuData
 End Function
 '查询用户
 Public Shared Function CheckUserID(ByVal enUser As UserInfoEntity) As DataTable
 '调用D层函数
 Dim dtUser As DataTable = sqlUserManage.CheckUserID(enUser)
 Return dtUser
 End Function

D层代码

  '组合查询
 '学生基本信息维护
 Public Shared Function CheckStuData(ByVal sqlstring As String) As DataTable
 Dim sqlhelper As New sqlHelper.sqlHelper
 Dim cmdtext As String = "select * from [T_StudentCard] where " + sqlstring
 Dim dtStuData As DataTable = sqlhelper.ExecSelect(cmdtext, CommandType.Text)
 Return dtStuData
 End Function
   '查询用户
 Public Shared Function CheckUserID(ByVal enUser As UserInfoEntity) As DataTable
 Dim sqlhelper As New sqlHelper.sqlHelper
 Dim cmdtext As String = "select UserID from [T_User] where [Level]<>@Levle"
 Dim sqlparas As SqlParameter() = {New SqlParameter("@Levle", "一般用户")}
 Dim dtUser As DataTable = sqlhelper.ExecSelect(cmdtext, CommandType.Text, sqlparas)
 Return dtUser
 End Function

细节处理

其实,到现在为止,组合查询就已经可以完成了,但是,软件设计的总是是全心全意为人民服务,所以就在U层又添了点代码。之所以“查询内容”使用的是ComboBox控件而不是TextBox,是希望它的形式可以根据“字段名”内容的变化而变化。比如“字段名”是“卡号”,那么就将ComboBox的下拉形式改为Simple,这样就可以让用户直接输入,在输入的同时还提醒用户只能输入数字。类似的设置代码如下:

Private Sub comboField1_Click(sender As Object, e As EventArgs) Handles comboField1.Click
        comboField1.Items.Clear() '每次单击事件都要清空,方式累加显示
        comboField1.Items.Add("")
        comboField1.Items.Add("卡号")
        comboField1.Items.Add("学号")
        comboField1.Items.Add("姓名")
        comboField1.Items.Add("性别")
        comboField1.Items.Add("系别")
        comboField1.Items.Add("年级")
        comboField1.Items.Add("班级")
        comboField1.Items.Add("教师")

    End Sub


    Private Sub comboCheck1_GotFocus(sender As Object, e As EventArgs) Handles comboCheck1.GotFocus
        If comboField1.Text = "卡号" Or comboField1.Text = "学号" Or comboField1.Text = "姓名" Then
            comboCheck1.DropDownStyle = ComboBoxStyle.Simple '变为文本框输入格式

        End If
        Select Case comboField1.Text
            
            Case "性别"
                comboCheck1.DropDownStyle = ComboBoxStyle.DropDownList  '变为下拉列表格式(不可输入)
                comboCheck1.Items.Clear()
                comboCheck1.Items.Add("")
                comboCheck1.Items.Add("男")
                comboCheck1.Items.Add("女")
            Case "系别"
                comboCheck1.DropDownStyle = ComboBoxStyle.DropDownList  '变为下拉列表格式(不可输入)
                comboCheck1.Items.Clear()
                comboCheck1.Items.Add("")
                comboCheck1.Items.Add("新闻")
                comboCheck1.Items.Add("心理")
                comboCheck1.Items.Add("计算机")
                comboCheck1.Items.Add("生科")
                comboCheck1.Items.Add("体育")
            Case "年级"
                comboCheck1.DropDownStyle = ComboBoxStyle.DropDownList  '变为下拉列表格式(不可输入)
                comboCheck1.Items.Clear()
                comboCheck1.Items.Add("")
                comboCheck1.Items.Add("1")
                comboCheck1.Items.Add("2")
                comboCheck1.Items.Add("3")
                comboCheck1.Items.Add("4")
            Case "班级"
                comboCheck1.DropDownStyle = ComboBoxStyle.DropDownList  '变为下拉列表格式(不可输入)
                comboCheck1.Items.Clear()
                comboCheck1.Items.Add("")
                comboCheck1.Items.Add("1")
                comboCheck1.Items.Add("2")
                comboCheck1.Items.Add("3")
                comboCheck1.Items.Add("4")
                comboCheck1.Items.Add("5")
            Case "教师"
                comboCheck1.DropDownStyle = ComboBoxStyle.DropDown  '变为既可输入又可选择的形式
                comboCheck1.Items.Clear()
                '将所有非一般用户的用户ID全部显示在下拉列表里
                '调用B层函数
                '实例化实体
                Dim enUser As New UserInfoEntity
                Dim dtUser As DataTable = UserManageBLL.CheckUserID(enUser)
                If dtUser.Rows.Count = 0 Then
                    MsgBox("没有教师记录!", , "提示")
                    Exit Sub
                Else
                    '将教师的用户ID显示在comboCheck1中
                    comboCheck1.Items.Add("")
                    For k = dtUser.Rows.Count To 1 Step -1
                        Dim UserID As String = dtUser.Rows(k - 1)(0)
                        comboCheck1.Items.Add(UserID)
                    Next
                End If


        End Select

以上只是第一组查询条件的设置,类似控件设置同上,所以就不全写出来了。

小结:

当然,到现在为止依然是不完美,不过还好,先这样吧。继续往下走吧。

你可能感兴趣的:(VB.NET)