2013-10-23 191 views
0

當我運行它提供了以下錯誤SQL UPDATE語句OleDbException

語法錯誤在UPDATE語句的程序。

在大膽

If (foundItem = True) Then 
    olDataConnection.Open() 
olCommand = New OleDbCommand("UPDATE Stock SET Product Name=" & itemName & "WHERE Price =" & itemprice & "'", olDataConnection) 
olCommand.ExecuteNonQuery() 

回答

0

使用parmeterized查詢,如

olCommand = New OleDbCommand("UPDATE Stock SET Product Name= ? WHERE Price =?", olDataConnection) 
olCommand.Parameters.AddWithValue("?", itemName); 
olCommand.Parameters.AddWithValue("?", itemprice); 

根據OleDbCommand不支持命名的參數

因此,在這種OleDbParameter對象添加到OleDbParameterCollection的順序必須直接對應於問號佔位符在命令文本的參數的位置。

0

顯示嘗試錯誤的語句改變

olCommand = New OleDbCommand("UPDATE Stock SET Product Name=" & itemName & "WHERE Price =" & itemprice & "'", olDataConnection) 

olCommand = New OleDbCommand("UPDATE Stock SET Product Name='" & itemName & "' WHERE Price =" & itemprice, olDataConnection)