带有Access数据库的VB更新命令

时间:2014-03-19 10:38:32

标签: vb.net ms-access syntax-error update-statement

 Function UpdateTableRow()
        sqlLink.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\will\Documents\Computing\ComputingProjectDatabase.accdb';"

    Try
        Dim sqlOrder As New OleDbCommand
        If sqlLink.State = ConnectionState.Closed Then
            sqlLink.Open()
        End If

        ' Creating the command and its parameter here before entering the loop to avoid a continue'
        ' create and destroy pattern for the OleDbCommand'

        sqlOrder.CommandText = "UPDATE StockSystem SET [Stock Price] =?, [Stock Size] =?, [Stock Quantity] =?, [Stock Category] =?, WHERE [Stock ID] =?"
        sqlOrder.Connection = sqlLink

        sqlOrder.Parameters.AddWithValue("@StockCategory", 0)
        sqlOrder.Parameters.AddWithValue("@StockQuantity", 0)
        sqlOrder.Parameters.AddWithValue("@StockSize", 0)
        sqlOrder.Parameters.AddWithValue("@StockPrice", 0)
        sqlOrder.Parameters.AddWithValue("@row", 0)

        Dim rows = DataGridView1.Rows

        For Each row In rows

            sqlOrder.Parameters("@row").Value = row.Cells(0).Value
            sqlOrder.Parameters("@StockPrice").Value = row.Cells(1).Value
            sqlOrder.Parameters("@StockSize").Value = row.Cells(2).Value
            sqlOrder.Parameters("@StockQuantity").Value = row.Cells(3).Value
            sqlOrder.Parameters("@StockCategory").Value = row.Cells(4).Value

            sqlOrder.ExecuteNonQuery()

        Next

        MsgBox("Data Updated.")

        'Problem with the update parameters, works for DELETE FUNCTION, needs to be changed to fit an UPDATE FUNCTION

    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        sqlLink.Close()

    End Try
    Return DataGridView1.SelectedRows
    DataGridView1.Refresh()
End Function
嘿,所以你可以看到我在这里有一些很好的代码。只有它没有100%的工作。我不断被告知Syntax Error in UPDATE statement。现在我非常确定更新语句是正确的,因为没有参数,如果代码没有问题(诚然没有参数,它根本不起作用)。我已经坚持了大约一个星期,所以任何帮助都会很棒>。<

我得到的第二个错误是,因为我使用Access,所以有一个"空白"在表底部的行。我认为像(行-1)这样的东西会起作用,只有VB 2010根本就不会这样。

正如我所说,任何帮助都会很棒。

2 个答案:

答案 0 :(得分:2)

您在,之前添加了WHERE。这是UPDATE语句中语法错误的原因

试试这个

sqlOrder.CommandText = "UPDATE StockSystem SET [Stock Price] =?, [Stock Size] =?, [Stock Quantity] =?, [Stock Category] =? WHERE [Stock ID] =?"

另一方面,我不确定你的意思。这个空行?

enter image description here

只是允许用户插入新行。它不是空行,因此您无需使用 rowindex - 1

引用行

更新:尝试更正您的代码。我会这样想:

Try

    If sqlLink.State = ConnectionState.Closed Then
        sqlLink.Open()
    End If

    Dim rows = DataGridView1.Rows

    For Each row In rows

        Dim sqlOrder As New OleDbCommand("UPDATE StockSystem SET [Stock Price] =?, [Stock Size] =?, [Stock Quantity] =?, [Stock Category] =? WHERE [Stock ID] =?", sqlLink)            

        sqlOrder.Parameters.AddWithValue("@StockPrice", row.Cells(1).Value)
        sqlOrder.Parameters.AddWithValue("@StockSize", row.Cells(2).Value)
        sqlOrder.Parameters.AddWithValue("@StockQuantity", row.Cells(3).Value)
        sqlOrder.Parameters.AddWithValue("@StockCategory", row.Cells(4).Value)
        sqlOrder.Parameters.AddWithValue("@row", row.Cells(0).Value)

        sqlOrder.ExecuteNonQuery()

    Next

    MsgBox("Data Updated.")

Catch ex As Exception
    MsgBox(ex.Message)
Finally
    sqlLink.Close()
End Try

答案 1 :(得分:0)

尝试删除 Where 之前的逗号,更改此内容:

([Stock Category] =?, WHERE [Stock ID] =?)

对此:

([Stock Category] =? WHERE [Stock ID] =?)