-1

这些有什么问题?

我的模块是:

Imports System.Data.OleDb
Module Module1
    Public con As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\CITeval\system7\system7\evaluation.mdb")
    Public da As OleDbDataAdapter
    Public dr As OleDbDataReader
    Public cmd As OleDbCommand
    Public ds = New DataSet
    Public CurrentRow As Integer
    Public sql As String
End Module

btn更新

Try
    Dim Str As String
    Str = "update studentsrecord set IDNumber="
    Str += """" & txtIDNumber.Text & """"
    Str += " where IDNumber="
    Str += txtIDNumber.Text.Trim()
    con.Open()
    cmd = New OleDbCommand(Str, con)
    cmd.ExecuteNonQuery()
    con.Close()
    con.Open()
    Str = "update studentsrecord set FirstName="
    Str += """" & txtfirst.Text & """"
    Str += " where IDNumber="
    Str += txtIDNumber.Text.Trim()
    con.Open()
    cmd = New OleDbCommand(Str, con)
    cmd.ExecuteNonQuery()
    con.Close()
    con.Open()
    Str = "update studentsrecord set LastName="
    Str += """" & txtlast.Text & """"
    Str += " where IDNumber="
    Str += txtfirst.Text.Trim()
    cmd = New OleDbCommand(Str, con)
    cmd.ExecuteNonQuery()
    con.Close()
    con.Open()
    Str = "UPDATE studentsrecord set Course="
    Str += """" & cbocourse.Text & """"
    Str += " where IDNumber="
    Str += txtIDNumber.Text.Trim()
    cmd = New OleDbCommand(Str, con)
    cmd.ExecuteNonQuery()
    con.Close()
    con.Open()
    Str = "update studentsrecord set Password="
    Str += """" & txtpassword.Text & """"
    Str += " where IDNumber="
    Str += txtIDNumber.Text.Trim()
    cmd = New OleDbCommand(Str, con)
    cmd.ExecuteNonQuery()
    con.Close()
    ds.Clear()
    da = New OleDbDataAdapter("SELECT * FROM studentsrecord ORDER BY ID", con)
    da.Fill(ds, "evaluation")
    MsgBox("Updated Successfully...")
Catch ex As Exception
    MsgBox(ex.Message & "," & ex.Source)
Finally
    If con.State = ConnectionState.Open Then con.Close()
End Try
4

1 回答 1

1

您不必为每个字段发出单独的 UPDATE 语句,您可以在单个 UPDATE 语句中更新多个字段。更好的选择是使用参数化查询而不是连接字符串。

在你的 TRY/CATCH 块内试试这个:

Dim Str As String
Str = "update studentsrecord set FirstName = @FirstName, LastName = @LastName,  Course = @Course, Password = @Password  where IDNumber = @IDNumber "
cmd = New OleDbCommand(Str, con)
cmd.Parameters.AddWithValue("@FirstName", txtfirst.Text)
cmd.Parameters.AddWithValue("@LastName", txtlast.Text)
cmd.Parameters.AddWithValue("@Course", cbocourse.Text)
cmd.Parameters.AddWithValue("@Password", txtpassword.Text)
cmd.Parameters.AddWithValue("@IDNumber", txtIDNumber.Text.Trim())

con.Open()
cmd.ExecuteNonQuery()

ds.Clear()
da = New OleDbDataAdapter("SELECT * FROM studentsrecord ORDER BY ID", con)
da.Fill(ds, "evaluation")

MsgBox("Updated Successfully...")
于 2013-09-04T14:20:38.050 回答