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 根本不喜欢这样。
正如我所说,任何帮助都会很棒。