这些错了吗?
我的模块是:
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 update
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
答案 0 :(得分: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...")