0
 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。现在我很确定更新语句是正确的,因为没有参数 if 代码没有问题(诚然,没有参数它根本不起作用)。我已经坚持了大约一个星期,所以任何帮助都会很棒>。<

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

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

4

2 回答 2

2

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

尝试这个

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

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

在此处输入图像描述

它只是允许用户插入新行。它不是空行,因此您不需要使用 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
于 2014-03-19T10:42:50.663 回答
0

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

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

对此:

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

于 2014-03-19T10:49:55.910 回答