1

我得到一个 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

连接关闭()

任何帮助,将不胜感激。谢谢!

4

2 回答 2

2

您不订购更新命令。

Dim statement As String = "UPDATE tblPatientInfo SET bottle_used='" & bottlecount & "' WHERE room_number= '" & roomhold & "'"
于 2012-09-11T15:35:35.647 回答
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
于 2012-09-11T15:49:46.793 回答