开发实践教程1:试卷生成系统6.14 更新试卷信息(FormPaperInfoUpdate)

Posted VB.Net

tags:

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

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

更新用户自己创建的试卷的信息。考虑到当试卷是完善的试卷(既包含试卷信息,又包含具体题目的试卷)时,如果修改数据,可能会导致试卷信息和题目信息不一致,从而引发错误,因此这里仅能修改试卷名称和试卷说明。

窗体设计如下:

 图1-24

具体代码如下:

Imports System.Data.SqlClient

Public Class FormPaperInfoUpdate
    Dim connection As SqlConnection

    Dim dgvRowIndex As Integer
    Dim id As Integer
    Dim paperName As String
    Dim paperInfo As String

    Dim blUpdate As Boolean

    Dim fTestPaperManager As FormTestPaperManager

    Sub New(ByVal dgvRowIndex As Integer, ByVal id As Integer, ByVal paperName As String, ByVal paperInfo As String)

        InitializeComponent()

        Me.dgvRowIndex = dgvRowIndex
        Me.id = id
        Me.paperName = paperName
        Me.paperInfo = paperInfo
    End Sub
    Private Sub FormPaperInfoUpdate_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtName.Text = paperName
        txtInfo.Text = paperInfo
        blUpdate = False

        fTestPaperManager = Me.Owner.ActiveMdiChild

        connection = New SqlConnection(databaseConnString)
        connection.Open()
    End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        Dim errMsg As String
        errMsg = checkData()
        If errMsg <> "" Then
            MessageBox.Show(errMsg, "设置试卷错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        Dim sql As String
        Dim command As New SqlCommand()
        command.Connection = connection
        command.CommandText = "update 用户试卷表 set 试卷名称='" & txtName.Text.Trim & "',试卷说明='" & txtInfo.Text.Trim & "' where 编号=" & id
        Try
            command.ExecuteNonQuery()
            blUpdate = True
            MessageBox.Show("更新成功")

            fTestPaperManager.dgv.Rows(dgvRowIndex).Cells(1).Value = txtName.Text.Trim

        Catch ex As Exception
            blUpdate = False
            MessageBox.Show("更新失败,错误原因:" & ex.Message)
        End Try
    End Sub

    Private Function checkData() As String
        If txtName.Text.Trim.Length < 6 Then
            Return "试卷名称请保持6个字以上"
        End If

        If txtInfo.Text.Trim = "" Then
            Return "未设置试卷说明"
        End If

        If txtName.Text.Trim = paperName And txtInfo.Text.Trim = paperInfo Then
            Return "试卷名称和试卷说明并未修改"
        End If

        Return ""
    End Function

    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.14 更新试卷信息(FormPaperInfoUpdate)的主要内容,如果未能解决你的问题,请参考以下文章

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

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

开发实践教程1:试卷生成系统6.16 试卷类型管理(FormPaperType)

开发实践教程1:试卷生成系统6.21 用户信息(FormSelfInfo)

开发实践教程1:试卷生成系统6.9 题型选择(FormSingleExamType)

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