VB.NET机房收费系统——组合查询

        在VB.NET 机房收费系统的组合查询中我写了完了一条线,然后看了其他同学对于组合查询的代码我感觉自己的代码很幼稚,不过还是想发表一下这个代码花了我半天的时间完成的。接下来就贴上图和代码。

 

     组合查询的窗体:

                          VB.NET机房收费系统——组合查询_第1张图片

U层代码:

Public ClassUIFrmOperatorWorkRecord

Private SubUIFrmOperatorWorkRecord_Load(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles MyBase.Load

        ’定义字符串数组

 DimstrFieldName(6) As String

        Dim strOperator(3) As String

        Dim strCompositionRelation(1) As String

       

DimIntTemp As Integer

 

        '给字符串数组赋值

        For IntTemp = 0 To 6

            Select Case IntTemp

                Case 0

                    strFieldName(0) ="教师"

                    strOperator(0) ="="

                    strCompositionRelation(0) ="或"

                Case 1

                    strFieldName(1) ="级别"

                    strOperator(1) =">"

                    strCompositionRelation(1) ="与"

                Case 2

                    strFieldName(2) ="登录日期"

                    strOperator(2) ="<"

                Case 3

                    strFieldName(3) ="登录时间"

                    strOperator(3) ="="

                Case 4

                    strFieldName(4) ="注销日期"

                Case 5

                    strFieldName(5) ="注销时间"

                Case 6

                    strFieldName(6) ="机器名"

            End Select

        Next IntTemp

     

        '控件增加字段

       cboFieldName1.Items.AddRange(strFieldName)

       cboFieldName2.Items.AddRange(strFieldName)

       cboFieldName3.Items.AddRange(strFieldName)

       cboOperator1.Items.AddRange(strOperator)

       cboOperator2.Items.AddRange(strOperator)

       cboOperator3.Items.AddRange(strOperator)

    cboCompositionRelation1.Items.AddRange(strCompositionRelation)

       cboCompositionRelation2.Items.AddRange(strCompositionRelation)

 

    End Sub

 

    Private Sub BtnCheck_Click(ByVal sender AsSystem.Object, ByVal e As System.EventArgs) Handles BtnCheck.Click

        Dim ECombination As NewEntity.Combination

        Dim BCombination As NewBLL.BLLOperatorWorkRecord

        Dim dt As New DataTable

        '把信息传给ECombination

        ECombination.FieldName1 =Trim(cboFieldName1.Text)

        ECombination.FieldName2 =Trim(cboFieldName2.Text)

        ECombination.FieldName3 =Trim(cboFieldName3.Text)

        ECombination.Operator1 =Trim(cboOperator1.Text)

        ECombination.Operator2 =Trim(cboOperator2.Text)

        ECombination.Operator3 =Trim(cboOperator3.Text)

        ECombination.CompositionRelation1 =Trim(cboCompositionRelation1.Text)

        ECombination.CompositionRelation2 =Trim(cboCompositionRelation2.Text)

        ECombination.ChcekContent1 =Trim(txtChcekContent1.Text)

        ECombination.ChcekContent2 =Trim(txtChcekContent2.Text)

        ECombination.ChcekContent3 =Trim(txtChcekContent3.Text)

        '组合查询

        dt =BCombination.CombinationCheck(ECombination)

       '把查到的结果传给gvwViewOperatorWorkRecord控件显示出来

        gvwViewOperatorWorkRecord.DataSource =dt

    End Sub

 

    Private Sub BtnExit_Click(ByVal sender AsSystem.Object, ByVal e As System.EventArgs) Handles BtnExit.Click

        Me.Hide()

    End Sub

End Class

 

B层代码

Public ClassBLLOperatorWorkRecord

    ''' <summary>

    ''' 组合查询

    ''' </summary>

    ''' <paramname="Combination"></param>

    ''' <returns></returns>

    ''' <remarks></remarks>

    Function CombinationCheck(ByVal CombinationAs Entity.Combination) As DataTable

        Dim ECombination As NewEntity.Combination '定义一个实体类

        Dim DCombiantion As NewDAL.DALWorkLogInfo '定义一个DALWorkLogInfo来调用D层的方法

        Dim strSqlString As String '定义一个存SQL语句

        Dim dt As New DataTable'定义一个DataTable来存储查询出来的语句

        ECombination = Combination '把传过来的值传给ECombination

        strSqlString =CombinationSqlString(ECombination) '调用 CombinationSqlString函数来拼接sql语句

        dt =DCombiantion.CheckWorkLogInfo(strSqlString)'调用D层的CheckWorkLogInfo方法来查询,并把查询到的结果传给dt

        Return dt

    End Function

    ''' <summary>

    ''' 改变把中文字符编程英文

    ''' </summary>

    ''' <paramname="Field"></param>

    ''' <returns></returns>

    ''' <remarks></remarks>

    Function ChangeField(ByVal Field As String)As String

        Select Case Field

            Case "教师"

                Field = " OperatorID  "

            Case "级别"

                Field = "  OperatorLevel "

            Case "登录日期"

                Field = "  LogonDate "

            Case "登录时间"

                Field = "  LogonTime "

            Case "注销日期"

                Field = "  LogOutDate "

            Case "注销时间"

                Field = "  LogOutTime "

            Case "机器名"

                Field = "  OperatorUseComputer  "

            Case "或"

                Field = "  or "

            Case "与"

                Field = "  and "

        End Select

        Return Field

    End Function

    ''' <summary>

    ''' 组合条件

    ''' </summary>

    ''' <paramname="Combination"></param>

    ''' <returns></returns>

    ''' <remarks></remarks>

    Function CombinationSqlString(ByValCombination As Entity.Combination) As String

        Dim ECombination As NewEntity.Combination '定义一个实体类

 

        '定义字符串存储查询的内容

        Dim strSqlString1 As String

        Dim strSqlString2 As String

        Dim strSqlString3 As String

        Dim strSqlAll As String

 

        ECombination = Combination

        '拼接sql字符串

        ECombination.FieldName1 =ChangeField(Combination.FieldName1)

        ECombination.FieldName2 =ChangeField(Combination.FieldName2)

        ECombination.FieldName3 =ChangeField(Combination.FieldName3)

        ECombination.ChcekContent1 ="'" + Combination.ChcekContent1 + "'"

        ECombination.ChcekContent2 ="'" + Combination.ChcekContent2 + "'"

        ECombination.ChcekContent3 ="'" + Combination.ChcekContent3 + "'"

        ECombination.CompositionRelation1 =ChangeField(Combination.CompositionRelation1)

        ECombination.CompositionRelation2 =ChangeField(Combination.CompositionRelation2)

        strSqlString1 = ECombination.FieldName1+ ECombination.Operator1 + ECombination.ChcekContent1 +ECombination.CompositionRelation1

        strSqlString2 = ECombination.FieldName2+ ECombination.Operator2 + ECombination.ChcekContent2 +ECombination.CompositionRelation2

        strSqlString3 = ECombination.FieldName3+ ECombination.Operator3 + ECombination.ChcekContent3

 

        '检查某些字符串为空的情况

        Select Case True

            Case ECombination.FieldName2 ="" And ECombination.CompositionRelation1 <> ""

                strSqlString1 =ECombination.FieldName1 + ECombination.Operator1 + ECombination.ChcekContent1

                strSqlString2 = ""

                strSqlString3 = ""

            Case ECombination.FieldName3 ="" And ECombination.CompositionRelation2 <> ""

                strSqlString2 =ECombination.FieldName2 + ECombination.Operator2 + ECombination.ChcekContent2

                strSqlString3 = ""

                strSqlAll =AddString(strSqlString1, strSqlString2, strSqlString3)

            CaseECombination.CompositionRelation1 = ""

                strSqlString1 =ECombination.FieldName1 + ECombination.Operator1 + ECombination.ChcekContent1

                strSqlString2 = ""

                strSqlString3 = ""

            CaseECombination.CompositionRelation2 = ""

                strSqlString2 =ECombination.FieldName2 + ECombination.Operator2 + ECombination.ChcekContent2

                strSqlString3 = ""

        End Select

        '把查询字符串拼接在一起

        strSqlAll = AddString(strSqlString1,strSqlString2, strSqlString3)

            Return strSqlAll

    End Function

    ''' <summary>

    ''' 字符串拼接

    ''' </summary>

    ''' <paramname="str1"></param>

    ''' <paramname="str2"></param>

    ''' <paramname="str3"></param>

    ''' <returns></returns>

    ''' <remarks></remarks>

    Function AddString(ByVal str1 As String,ByVal str2 As String, ByVal str3 As String) As String

        Dim strAllString As String

        strAllString = str1 + str2 + str3

        Return strAllString

    End Function

End Class

 

D层:

  ''' <summary>

    ''' 组合查询

    ''' </summary>

    '''

    ''' <returns></returns>

    ''' <remarks></remarks>

    Function CheckWorkLogInfo(ByValstrCombinationSQL As String) As DataTable

        Dim strSql As String ="select  OperatorID as教师,OperatorLevel as 级别,LogonDate as 登录日期,LogonTime as 登录时间,LogOutDate as注销日期,LogOutTime as 注销时间,OperatorUseComputer as 机器名 from WorkLog_Info where" + strCombinationSQL‘SQL语句

        Dim cmd As SqlCommand = NewSqlCommand(strSQL, conn)’连接数据库

        Dim sqlda As NewSqlDataAdapter‘建一个SQLDataAdapter

        Dim ds As New DataSet’定义一个新的DataSet

        Dim dt As NewDataTable‘定义一个新的DataTable

        Try

            conn.Open()’打开数据库

           sqlda.SelectCommand = cmd‘把cmd传给da

            sqlda.Fill(dt)’填充dt

            Return dt

        Catch ex As Exception

            Return dt

        Finally

            cmd.Dispose()

            conn.Close()

        End Try

 

    End Function

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