1

「SQL ステートメントの末尾にセミコロン (;) がありません」という OleDbException が発生しました。UPDATEステートメントが正しいかどうか混乱しました。私のコードでは、ボタンを使用して 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

Conn.Close()

どんな助けでも大歓迎です。ありがとう!

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

UPDATEORDER 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 に答える