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

在VB.NET机房收费系统个人版的过程中,又遇到了组合查询了。机房收费系统的组合查询是所有组合查询类型中相对比较繁琐和困难的。当然,在.NET的组合查询中,我用的是拼接字符串。

下面以机房收费系统中“学生基本信息维护”为例,讲一下我是如何实现组合查询


这是“学生基本信息维护”的界面:

我先说一下解决的思路:因为是用三层的思想,所以我们就需要考虑解决方法和各个层应该放什么内容。我 用的方法是拼接字符串,所以首先U层:要在U层里面定义一个函数,实现文本框里面中英文的转换;然后,定义一个字符串,根据组合关系框的不同情况依次拼写字符串——

Public Class FormUI_ProtectStudentInfo
    '退出按钮
    Private Sub btnCancel_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnCancel.Click
        FormUI_Main.Show()
        Me.Hide()
    End Sub

    '查询按钮
    Private Sub btnQuery_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnQuery.Click
        '清空DataGridView控件的内容
        DataGridView1.DataSource = vbNull

        '字段名不允许为空
        If cmbFieldBox1.Text.Trim() = "" Then
            MessageBox.Show("对不起,字段名不允许为空,请您选择字段名","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Warning)
            Exit Sub
        End If

        '操作符不允许为空
        If cmbOperateBox1.Text.Trim() = "" Then
            MessageBox.Show("对不起,操作符不允许为空,请您选择操作符",MessageBoxIcon.Warning)
            Exit Sub
        End If

        '要查询内容不允许为空
        If txtQueryContent1.Text.Trim() = "" Then
            MessageBox.Show("对不起,请您输入要查询内容,要查询内容不允许为空",MessageBoxIcon.Warning)
            Exit Sub
        End If


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

        '当组合关系框均为空时
        If cmbConbineBox1.Text = "" Then
            sqlstring = GetEnglish(cmbFieldBox1.Text) + cmbOperateBox1.Text + txtQueryContent1.Text

            '当第一个组合关系框为“或”第二个框为空时
        ElseIf cmbConbineBox1.Text = "或" And cmbCombineBox2.Text = "" Then
            sqlstring = GetEnglish(cmbFieldBox1.Text) + cmbOperateBox1.Text + txtQueryContent1.Text Or GetEnglish(cmbFieldBox1.Text) + cmbOperateBox2.Text + txtQueryContent2.Text

            '当第一个组合关系框为“与”第二个框为空时
        ElseIf cmbConbineBox1.Text = "与" And cmbCombineBox2.Text = "" Then
            sqlstring = GetEnglish(cmbFieldBox1.Text) + cmbOperateBox2.Text + txtQueryContent1.Text And GetEnglish(cmbFieldBox1.Text) + cmbOperateBox2.Text + txtQueryContent2.Text

            '当第一个框为“或”,第二个框为“或”时
        ElseIf cmbConbineBox1.Text = "或" And cmbCombineBox2.Text = "或" Then
            sqlstring = GetEnglish(cmbFieldBox1.Text) + cmbOperateBox1.Text + txtQueryContent1.Text Or GetEnglish(cmbFieldBox2.Text) + cmbOperateBox2.Text + txtQueryContent2.Text Or GetEnglish(cmbFieldBox3.Text) + cmdOperateBox3.Text + txtQueryContent3.Text

            '当第一个框为“与”第二个框为“与”时
        ElseIf cmbConbineBox1.Text = "与" And cmbCombineBox2.Text = "与" Then
            sqlstring = GetEnglish(cmbFieldBox1.Text) + cmbOperateBox1.Text + txtQueryContent1.Text And GetEnglish(cmbFieldBox2.Text) + cmbOperateBox2.Text + txtQueryContent2.Text And GetEnglish(cmbFieldBox3.Text) + cmdOperateBox3.Text + txtQueryContent3.Text

            '当第一个框为“或”第二个框为“与”
        ElseIf cmbConbineBox1.Text = "或" And cmbCombineBox2.Text = "与" Then
            sqlstring = GetEnglish(cmbFieldBox1.Text) + cmbOperateBox1.Text + txtQueryContent1.Text Or GetEnglish(cmbFieldBox2.Text) + cmbOperateBox2.Text + txtQueryContent2.Text And GetEnglish(cmbFieldBox3.Text) + cmdOperateBox3.Text + txtQueryContent3.Text

            '当第一个框为“与”第二个框为“或”
        ElseIf cmbConbineBox1.Text = "与" And cmbCombineBox2.Text = "或" Then
            sqlstring = GetEnglish(cmbFieldBox1.Text) + cmbOperateBox1.Text + txtQueryContent1.Text And GetEnglish(cmbFieldBox2.Text) + cmbOperateBox2.Text + txtQueryContent2.Text Or GetEnglish(cmbFieldBox3.Text) + cmdOperateBox3.Text + txtQueryContent3.Text

        End If


        '定义一个B层对象
        Dim uB As New BLL.BLL_ProtectStudentInfo
        Dim a As DataTable
        a = uB.query_st(sqlstring)
        If a.Rows.Count() > 0 Then
            DataGridView1.DataSource = a
        Else
            MessageBox.Show("没有记录",MessageBoxIcon.Information)
        End If
    End Sub

    Public Function GetEnglish(ByVal strControl As String) As String
        Select Case (strControl)
            Case "卡号"
                Return "card_no"
            Case "学号"
                Return "student_id"
            Case "性别"
                Return "st_sex"
            Case "姓名"
                Return "st_name"
            Case "年级"
                Return "st_grade"
            Case "院系"
                Return "st_department"
            Case "班级"
                Return "st_class"
            Case Else
                Return ""
        End Select
    End Function
End Class

以上便是U层的情况,当然B层很简单了,只要定义一个函数,返回D层的查询结果就可以了,记住,在这个过程中,传递的是我们自己定义的拼接的字符串哦

Public Class BLL_ProtectStudentInfo
Public Function query_st(ByVal sqlstring As String) As DataTable
        '定义一个D层对象
        Dim bD As New DAL.DAL_ProtectStudentInfo
        Dim dt As New DataTable
        dt = bD.Query_ST(sqlstring)
        Return dt
    End Function
End Class

D层就是根据所拼接的字符串,一个sql语句便搞定啦

Imports System.Data.sqlClient
Public Class DAL_ProtectStudentInfo
    '数据库连接语句
    Dim str As String = "server=.;database=Computer room Charging System;integrated security=sspi"
    Dim conn As sqlClient.sqlConnection

    '构造函数,建立连接,打开数据库
    Public Sub New()
        conn = New sqlClient.sqlConnection
        conn.ConnectionString = str
        conn.Open()
    End Sub

    '定义一个函数
    Public Function Query_ST(ByVal sqlstring As String) As DataTable
        'sql语句
        Dim sqlstr As String
        sqlstr = "select * from Student where " + sqlstring
        '执行
        Dim myAdapter As sqlDataAdapter = New sqlDataAdapter(sqlstr,conn)
        Dim myTable As New DataTable()
        myAdapter.Fill(myTable)
        Return myTable
    End Function

End Class

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强制返回为文本 --------------------------...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办法, Format 或者FormatDateTime 竟然结果和...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace My ‘全局错误处理,新的解决方案直接...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用的爽呀,这篇文章写与2011年,看来我以前没...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选中的单元格进行处理 Dim m As Range, t...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integ...