我得到一个 OleDbException 说“缺少 SQL 语句末尾的分号 (;)”。我对 UPDATE 语句是否正确感到困惑。在我的代码中,我使用按钮测试更新语句。谁能帮我?我想给变量加 1 以增加它的值;瓶数。我正在使用 Microsoft Access 数据库。Primary id 是 ID,然后 Unique 是 room_number。
这是我的代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'====test number of bottles=============
Dim bottlecount As Integer 'variable used in order to increase the value of no. of bottle/s used
bottlecount = Form3.lblBottle.Text
bottlecount += 1
Form3.lblBottle.Text = bottlecount
roomhold = 1
Dim statement As String = "UPDATE tblPatientInfo SET bottle_used='" & bottlecount & "' WHERE room_number= '" & roomhold & "' ORDER BY ID ;"
Dim cmd As New OleDbCommand
With cmd
.CommandText = statement
.Connection = Conn
Conn.Open()
.ExecuteNonQuery()
End With
Conn.Close()
End Sub
===应用更改后========== 这是我的代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bottlecount As Integer = CInt(Form3.lblBottle.Text) + 1
Form3.lblBottle.Text = bottlecount
roomhold = 1
Dim statement As String = "UPDATE tblPatientInfo SET bottle_used = @bottlecount"
statement &= "WHERE room_number = @roomhold"
Dim cmd As New OleDbCommand
With cmd
.Connection = Conn
.CommandType = CommandType.Text
.CommandText = statement
.Parameters.AddWithValue("@bottlecount", bottlecount)
.Parameters.AddWithValue("@roomhold", roomhold)
Conn.Open()
.ExecuteNonQuery()
End With
连接关闭()
任何帮助,将不胜感激。谢谢!