开发实践教程1:试卷生成系统6.13 试卷搜索(FormPaperManagerQuery)

Posted VB.Net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开发实践教程1:试卷生成系统6.13 试卷搜索(FormPaperManagerQuery)相关的知识,希望对你有一定的参考价值。

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

设置试卷管理时搜索的相关条件。

窗体设计如下:

 图1-23

具体代码如下:

Imports System.Data.SqlClient

Public Class formPaperManagerQuery

    Dim connection As SqlConnection

    Dim lstExamType As New List(Of Integer)

    Dim lstTestPaperTypeB As New List(Of Integer)
    Dim lstTestPaperTypeM As New List(Of Integer)
    Dim lstTestPaperTypeS As New List(Of Integer)

    Dim fExam As FormTestPaperManager

    Private Sub formPaperManagerQuery_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        connection = New SqlConnection(databaseConnString)
        connection.Open()

        Call drawComboBox()

        fExam = Me.Owner.ActiveMdiChild
    End Sub

    Private Sub drawComboBox()
        Dim sql As String
        sql = "select 编号,类型名称 from 考试类型表一级"

        Dim command As New SqlCommand()

        command.CommandText = sql
        command.Connection = connection

        Dim sqlReader As SqlDataReader
        sqlReader = command.ExecuteReader()

        If sqlReader.HasRows Then
            Do While sqlReader.Read
                cbPaperTypeB.Items.Add(sqlReader.GetString(1))
                lstTestPaperTypeB.Add(sqlReader.GetInt32(0))
            Loop
        End If
        sqlReader.Close()
        If cbPaperTypeB.Items.Count > 0 Then cbPaperTypeB.SelectedIndex = 0

        If permissions = 0 Then
            ckCreateMan.Checked = False
            ckCreateMan.Visible = True
            txtCreateMan.Text = ""
            txtCreateMan.Visible = True
        Else
            ckCreateMan.Checked = False
            ckCreateMan.Visible = False
            txtCreateMan.Text = ""
            txtCreateMan.Visible = False
        End If

    End Sub
    Private Sub cbPaperTypeB_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbPaperTypeB.SelectedIndexChanged
        cbPaperTypeM.Items.Clear()
        lstTestPaperTypeM.Clear()
        cbPaperTypeS.Items.Clear()
        lstTestPaperTypeS.Clear()

        Dim paperTypeBIndex As Integer = cbPaperTypeB.SelectedIndex

        Dim sql As String
        sql = "select 编号,类型名称 from 考试类型表二级 where 一级类型=" & lstTestPaperTypeB(paperTypeBIndex)


        Dim command As New SqlCommand()

        command.CommandText = sql
        command.Connection = connection

        Dim sqlReader As SqlDataReader
        sqlReader = command.ExecuteReader()

        If sqlReader.HasRows Then
            Do While sqlReader.Read
                cbPaperTypeM.Items.Add(sqlReader(1))
                lstTestPaperTypeM.Add(sqlReader(0))
            Loop
        End If

        sqlReader.Close()

        If cbPaperTypeM.Items.Count > 0 Then cbPaperTypeM.SelectedIndex = 0
    End Sub

    Private Sub cbPaperTypeM_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbPaperTypeM.SelectedIndexChanged
        cbPaperTypeS.Items.Clear()
        lstTestPaperTypeS.Clear()

        Dim paperTypeMIndex As Integer = cbPaperTypeM.SelectedIndex

        Dim sql As String
        sql = "select 编号,类型名称 from 考试类型表三级 where 二级类型=" & lstTestPaperTypeM(paperTypeMIndex)

        Dim command As New SqlCommand()

        command.CommandText = sql
        command.Connection = connection

        Dim sqlReader As SqlDataReader
        sqlReader = command.ExecuteReader()

        If sqlReader.HasRows Then
            Do While sqlReader.Read
                cbPaperTypeS.Items.Add(sqlReader(1))
                lstTestPaperTypeS.Add(sqlReader(0))
            Loop
        End If

        sqlReader.Close()

        If cbPaperTypeS.Items.Count > 0 Then cbPaperTypeS.SelectedIndex = 0
    End Sub

    Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
        Dim SqlWhere As String = ""
        Dim subSqlWhere As String = ""

        Dim checkCondition As Boolean = False

        If ckTitle.Checked = True Then
            checkCondition = True
            If txtPaperTitle.Text.Trim = "" Then
                MessageBox.Show("试卷名称不能为空值")
                Exit Sub
            End If
            SqlWhere = getTitle()
        End If

        If ckPaperTypeB.Checked = True Or ckPaperTypeM.Checked = True Or ckPaperTypeS.Checked = True Then
            checkCondition = True
            subSqlWhere = getPaperType()
            SqlWhere = IIf(SqlWhere = "", subSqlWhere, SqlWhere & " And " & subSqlWhere)
        End If

        If permissions = 0 Then
            If ckCreateMan.Checked = True Then
                checkCondition = True
                If txtCreateMan.Text.Trim = "" Then
                    MessageBox.Show("用户姓名不能为空值")
                    Exit Sub
                End If
                subSqlWhere = getCreateMan()
                SqlWhere = IIf(SqlWhere = "", subSqlWhere, SqlWhere & " And " & subSqlWhere)
            End If
        Else
            subSqlWhere = "(用户试卷表.录入人ID=" & loginId & ")"
            SqlWhere = IIf(SqlWhere = "", subSqlWhere, SqlWhere & " And " & subSqlWhere)
        End If


        If checkCondition = False Then
            MessageBox.Show("必须勾选一个查询条件")
            Exit Sub
        End If

        fExam.customWhere = SqlWhere
        Me.Close()
    End Sub

#Region "设置各个条件下的查询语句"

    Private Function getTitle() As String
        Return "(用户试卷表.试卷名称 like '%" & txtPaperTitle.Text.Trim & "%')"
    End Function

    Private Function getPaperType() As String
        Dim typeId As Integer

        If ckPaperTypeS.Checked = True Then
            typeId = lstTestPaperTypeS(cbPaperTypeS.SelectedIndex)
            Return "(考试类型表三级.编号=" & typeId & ")"
        End If

        If ckPaperTypeM.Checked = True Then
            typeId = lstTestPaperTypeM(cbPaperTypeM.SelectedIndex)
            Return "(考试类型表二级.编号=" & typeId & ")"
        End If

        If ckPaperTypeB.Checked = True Then
            typeId = lstTestPaperTypeB(cbPaperTypeB.SelectedIndex)
            Return "(考试类型表一级.编号=" & typeId & ")"
        End If

    End Function

    Private Function getCreateMan() As String
        Return "(用户表.真实姓名 like '%" & txtCreateMan.Text.Trim & "%')"
    End Function

#End Region


    Private Sub ckPaperType_CheckedChanged(sender As Object, e As EventArgs) Handles ckPaperTypeB.CheckedChanged, ckPaperTypeM.CheckedChanged, ckPaperTypeS.CheckedChanged
        If ckPaperTypeB.Checked = True Then
            ckPaperTypeM.Checked = False
            ckPaperTypeS.Checked = False
        End If
        If ckPaperTypeM.Checked = True Then
            ckPaperTypeB.Checked = False
            ckPaperTypeS.Checked = False
        End If
        If ckPaperTypeS.Checked = True Then
            ckPaperTypeB.Checked = False
            ckPaperTypeM.Checked = False
        End If

    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

End Class

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

以上是关于开发实践教程1:试卷生成系统6.13 试卷搜索(FormPaperManagerQuery)的主要内容,如果未能解决你的问题,请参考以下文章

开发实践教程1:试卷生成系统6.11 选择考题(FormChooseExam)

开发实践教程1:试卷生成系统6.18用户查询(FormUserQuery)

开发实践教程1:试卷生成系统6.7 试卷生成(FormTestPaper)

开发实践教程1:试卷生成系统6.12 试卷管理(FormTestPaperManager)

开发实践教程1:试卷生成系统6.8 试卷信息(FormTestPaperInfo)

开发实践教程1:试卷生成系统6.10 载入试卷(FormLoadTestPaper)