我得到一个OleDbException,说“在SQL语句结束时缺少分号(;)”。如果UPDATE语句是否正确,我感到困惑。在我的代码中,我使用按钮测试更新语句。谁能帮我?我想为变量添加1以增加其值;瓶数。我正在使用Microsoft Access数据库。主要ID是ID,然后唯一是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
任何帮助将不胜感激。谢谢!
答案 0 :(得分:2)
您不订购更新命令。
Dim statement As String = "UPDATE tblPatientInfo SET bottle_used='" & bottlecount & "' WHERE room_number= '" & roomhold & "'"
答案 1 :(得分:1)
UPDATE
不允许有ORDER BY
条款。如果您希望更新为订单,请创建其子查询,其中包含更新的有序列表。使用ADONet时,请确保您的查询是参数。习惯使用SQLCommand and Parameters
。如果它适用于您,请尝试使用此编辑的代码
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
Dim roomhold AS Integer = 1
Dim statement As String = "UPDATE tblPatientInfo SET bottle_used = @bottlecount "
statement &= "WHERE room_number = @roomhold "
Using conn as New SqlConnection("ConnectionStringHere")
Using comm as New SqlCommand()
With comm
.Connection = conn
.CommandType = CommandType.Text
.CommandText = statement
.Parameters.AddWithValue("@bottlecount", bottlecount )
.Parameters.AddWithValue("@roomhold" , roomhold)
End With
Try
conn.Open()
comm.ExecuteNonQuery()
Catch (ex as SQlException)
' add message here '
Finally
conn.Close()
End Try
End Using
End Using
End Sub