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

Posted VB.Net

tags:

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

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

当试卷生成后在“试卷生成”界面调整试卷信息比较复杂,因此单独设置“试卷管理”,允许用户更新自己创建的试卷的信息(仅试卷名称和试卷说明),还提供“删除”试卷功能,执行删除时除了删除《用户试卷表》内的数据外,还将删除《用户试卷详表》内相关的数据。

窗体设计如下:

图1-22

在删除试卷时,使用了SqlTransaction类,通过此类可以在MS Sql数据库中生成的Transact-SQL 事务,可以执行一连串的sql操作,如果其中一个操作失败,那么可以将所有操作回滚,复原数据。SqlTransaction类的简单用法如下:

建立事务类:

        Dim st As SqlTransaction

获得事务对象:

        st = connection.BeginTransaction

设置SqlCommand的事务为刚定义的事务:

        command.Transaction = st

        Try

从 用户试卷表 中删除试卷信息:

            command.CommandText = "delete from 用户试卷表 where 编号=" & dgv.SelectedRows(0).Cells(0).Value

            command.ExecuteNonQuery()

从 用户试卷详表 中删除试卷对应的所有考题信息:

            command.CommandText = "delete from 用户试卷详表 where 试卷编号=" & dgv.SelectedRows(0).Cells(0).Value

            command.ExecuteNonQuery()

提交事务:

            st.Commit()

        Catch ex As Exception

如果发生错误,则还原事务:

            st.Rollback()

        End Try

具体代码如下:

Imports System.Data.SqlClient

Public Class FormTestPaperManager

    Dim connection As SqlConnection
    Dim ds As DataSet
    Dim adapter As SqlDataAdapter
    Dim dt As DataTable

    Const pagesize As Integer = 50
    Dim maxPage As Integer
    Dim currentPage As Integer

    Dim F_Main As FormMain

    Dim sqlMySelect As String
    Dim sqlMyFrom As String
    Dim sqlMyOrder As String
    Dim sqlMyWhere As String

    Dim sqlQuerySelect As String
    Dim sqlQueryFrom As String
    Dim sqlQueryOrder As String
    Dim sqlQueryWhere As String

    Dim sqlFlag As String

    Public customWhere As String

    Dim dgvCanResize As Boolean = False

    Private Sub FormTestPaperManager_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        connection = New SqlConnection(databaseConnString)
        connection.Open()

        F_Main = CType(Me.MdiParent, FormMain)

        sqlMySelect = "SELECT 用户试卷表.编号 as 编号, 用户试卷表.试卷名称 as 试卷名称, 用户试卷表.试卷说明 as 试卷说明, 考试类型表一级.类型名称 as 一级考试类型, 
                        考试类型表二级.类型名称 as 二级考试类型, 考试类型表三级.类型名称 as 三级考试类型, 用户试卷表.总分值 as 总分值, 
                        用户试卷表.考试时长 as 考试时长, 用户表.真实姓名 as 创建人, 用户试卷表.录入时间 as 创建时间,用户试卷表.是否有效 as 是否有效 "
        sqlMyFrom = "FROM (((用户试卷表 INNER JOIN 考试类型表三级 ON 用户试卷表.试卷类型 = 考试类型表三级.编号) INNER JOIN 
                    考试类型表二级 ON 考试类型表三级.二级类型 = 考试类型表二级.编号) 
                    INNER JOIN 考试类型表一级 ON 考试类型表二级.一级类型 = 考试类型表一级.编号) 
                    INNER JOIN 用户表 ON 用户试卷表.录入人ID = 用户表.编号 "
        If permissions = 0 Then
            sqlQueryWhere = ""
        Else
            sqlMyWhere = "where 用户试卷表.录入人ID=" & loginId
        End If
        sqlMyOrder = "order by 用户试卷表.录入时间 desc"


        sqlQuerySelect = "SELECT 用户试卷表.编号 as 编号, 用户试卷表.试卷名称 as 试卷名称, 用户试卷表.试卷说明 as 试卷说明, 考试类型表一级.类型名称 as 一级考试类型, 
                        考试类型表二级.类型名称 as 二级考试类型, 考试类型表三级.类型名称 as 三级考试类型, 用户试卷表.总分值 as 总分值, 
                        用户试卷表.考试时长 as 考试时长, 用户表.真实姓名 as 创建人, 用户试卷表.录入时间 as 创建时间,用户试卷表.是否有效 as 是否有效 "
        sqlQueryFrom = "FROM (((用户试卷表 INNER JOIN 考试类型表三级 ON 用户试卷表.试卷类型 = 考试类型表三级.编号) INNER JOIN 
                    考试类型表二级 ON 考试类型表三级.二级类型 = 考试类型表二级.编号) 
                    INNER JOIN 考试类型表一级 ON 考试类型表二级.一级类型 = 考试类型表一级.编号) 
                    INNER JOIN 用户表 ON 用户试卷表.录入人ID = 用户表.编号 "
        sqlQueryWhere = ""
        sqlQueryOrder = "order by 用户试卷表.录入时间 desc"

        sqlFlag = "my"
        currentPage = 0

        Call showGrid(sqlFlag)

    End Sub

    Public Sub showGrid(ByVal tableFlag As String)
        ds = New DataSet(tableFlag)
        Dim command As New SqlCommand()

        Dim sqlSelect As String = ""
        Dim sqlFrom As String = ""
        Dim sqlWhere As String = ""
        Dim sqlOrder As String = ""

        Select Case tableFlag
            Case "my"
                sqlSelect = sqlMySelect
                sqlFrom = sqlMyFrom
                sqlWhere = sqlMyWhere
                sqlOrder = sqlMyOrder

            Case "custom"
                sqlSelect = sqlQuerySelect
                sqlFrom = sqlQueryFrom
                sqlWhere = sqlQueryWhere
                sqlOrder = sqlQueryOrder

        End Select
        command.CommandText = "select count(*) " & sqlFrom & sqlWhere
        command.Connection = connection

        Dim count As Integer = command.ExecuteScalar


        F_Main.tsslInfo.Text = "数据总数:" & count & " 条"

        If count < pagesize Then
            tsbFirstPage.Enabled = False
            tsbPrevPage.Enabled = False
            tsbNextPage.Enabled = False
            tsbLastPage.Enabled = False
        Else
            tsbFirstPage.Enabled = True
            tsbPrevPage.Enabled = True
            tsbNextPage.Enabled = True
            tsbLastPage.Enabled = True
        End If

        command.CommandText = sqlSelect &
                        sqlFrom &
                        sqlWhere &
                        sqlOrder

        adapter = New SqlDataAdapter(command)

        maxPage = Math.Ceiling(count / pagesize)

        If currentPage > maxPage Then currentPage = maxPage

        adapter.Fill(ds, currentPage * pagesize, pagesize, tableFlag)
        dgv.DataSource = Nothing
        dgv.DataSource = ds.Tables(tableFlag)
        tslRecord.Text = (currentPage + 1).ToString & "/" & maxPage.ToString

        dgv.Columns(0).Visible = False
        dgv.Columns(2).Visible = False
        dgvCanResize = True

    End Sub

    Private Sub tsbMyPaper_Click(sender As Object, e As EventArgs) Handles tsbMyPaper.Click
        sqlFlag = "my"
        Call showGrid(sqlFlag)

        tsbEdit.Enabled = True
        tsbDelete.Enabled = True
    End Sub


    Private Sub tsbFirstPage_Click(sender As Object, e As EventArgs) Handles tsbFirstPage.Click
        currentPage = 0
        ds.Tables(sqlFlag).Clear()
        adapter.Fill(ds, currentPage * pagesize, pagesize, sqlFlag)
        dgv.DataSource = ds.Tables(sqlFlag)

        tslRecord.Text = (1).ToString & "/" & maxPage.ToString
    End Sub
    Private Sub tsbPrevPage_Click(sender As Object, e As EventArgs) Handles tsbPrevPage.Click
        If currentPage = 0 Then
            MessageBox.Show("已经是第一页了")
            Exit Sub
        End If
        currentPage -= 1
        ds.Tables(sqlFlag).Clear()
        adapter.Fill(ds, currentPage * pagesize, pagesize, sqlFlag)
        dgv.DataSource = ds.Tables(sqlFlag)
        tslRecord.Text = (currentPage + 1).ToString & "/" & maxPage.ToString
    End Sub
    Private Sub tsbNextPage_Click(sender As Object, e As EventArgs) Handles tsbNextPage.Click
        If currentPage = maxPage - 1 Then
            MessageBox.Show("已经是最后一页了")
            Exit Sub
        End If
        currentPage += 1
        ds.Tables(sqlFlag).Clear()
        Dim recordcount As Integer = adapter.Fill(ds, currentPage * pagesize, pagesize, sqlFlag)
        dgv.DataSource = ds.Tables(sqlFlag)
        tslRecord.Text = (currentPage + 1).ToString & "/" & maxPage.ToString
    End Sub
    Private Sub tsbLastPage_Click(sender As Object, e As EventArgs) Handles tsbLastPage.Click
        currentPage = maxPage - 1
        ds.Tables(sqlFlag).Clear()
        adapter.Fill(ds, currentPage * pagesize, pagesize, sqlFlag)
        dgv.DataSource = ds.Tables(sqlFlag)
        tslRecord.Text = (currentPage + 1).ToString & "/" & maxPage.ToString
    End Sub


    Private Sub tsbQuery_Click(sender As Object, e As EventArgs) Handles tsbQuery.Click
        Dim fQuery As New formPaperManagerQuery
        fQuery.ShowDialog(Me)

        If customWhere = "" Then
            Exit Sub
        End If

        sqlQueryWhere = "where " & customWhere

        sqlFlag = "custom"

        Call showGrid(sqlFlag)
    End Sub

    Private Sub tsbEdit_Click(sender As Object, e As EventArgs) Handles tsbEdit.Click
        If dgv.SelectedRows.Count < 1 Then
            MessageBox.Show("你还没有选择需要更新的试卷。", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End If
        Dim dgvRowIndex As Integer
        dgvRowIndex = dgv.SelectedRows(0).Index
        Dim id As Integer
        Dim paperName As String
        Dim paperInfo As String

        id = dgv.SelectedRows(0).Cells(0).Value
        paperName = dgv.SelectedRows(0).Cells(1).Value
        paperInfo = dgv.SelectedRows(0).Cells(2).Value

        Dim fPaperInfoUpdate As New FormPaperInfoUpdate(dgvRowIndex, id, paperName, paperInfo)
        fPaperInfoUpdate.ShowDialog(Me)

    End Sub

    Private Sub tsbDelete_Click(sender As Object, e As EventArgs) Handles tsbDelete.Click
        If dgv.SelectedRows.Count < 1 Then
            MessageBox.Show("你还没有选择需要删除的试卷。", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End If

        Dim paperIndex As Integer
        paperIndex = dgv.SelectedRows(0).Index

        If MessageBox.Show("你确定要删除试卷 " & dgv.SelectedRows(0).Cells(1).Value & " ?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) = DialogResult.Cancel Then
            Exit Sub
        End If

        Dim command As New SqlCommand()
        command.Connection = connection

        Dim st As SqlTransaction
        st = connection.BeginTransaction
        command.Transaction = st

        Try
            command.CommandText = "delete from 用户试卷表 where 编号=" & dgv.SelectedRows(0).Cells(0).Value
            command.ExecuteNonQuery()
            command.CommandText = "delete from 用户试卷详表 where 试卷编号=" & dgv.SelectedRows(0).Cells(0).Value
            command.ExecuteNonQuery()
            st.Commit()
            MessageBox.Show("删除试卷成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Call showGrid(sqlFlag)
        Catch ex As Exception
            st.Rollback()
            MessageBox.Show("删除试卷失败。失败原因:" & vbCrLf & ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


    End Sub

    Private Sub tsbHome_Click(sender As Object, e As EventArgs) Handles tsbHome.Click
        F_Main.tsslInfo.Text = ""
        Me.Close()
    End Sub
End Class

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

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

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

开发实践教程1:试卷生成系统6.3 主界面(FormMain)

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

开发实践教程1:试卷生成系统4 类

开发实践教程1:试卷生成系统 1 前言和目录

开发实践教程1:试卷生成系统6.17 用户管理(FormUserList)

开发实践教程1:试卷生成系统6.4 考题管理(FormExam)