2011-04-01 63 views
0

無法使用下面的功能與值更新訪問數據網格

Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click 


Dim cn As New OleDbConnection(Con) 
     Dim objUpdateCommand As New OleDbCommand 
     Dim objDataAdapter As New OleDbDataAdapter 
     cn.Open() 
     Const SQLExpression As String = "UPDATE product SET Value = @pValue where owner = @powner and Item = @pItem;" 
     objUpdateCommand = New OleDbCommand(SQLExpression, cn) 

     With objUpdateCommand 
      .Parameters.Add("@powner", OleDbType.VarChar, 10, "owner") 
      .Parameters.Add("@pItem", OleDbType.VarChar, 8, "Item") 
      .Parameters.Add("@pValue", OleDbType.VarChar, 10, "Value") 
     End With 

     objDataAdapter.UpdateCommand = objUpdateCommand 

     MsgBox("Table Updated") 

End Sub 

回答

0

設置UpdateCommand屬性不執行命令來更新表中的訪問。您需要調用表格適配器的更新方法。

查看更多信息here

+0

我該怎麼做? – user688175 2011-04-01 19:55:00

0

你不必有一個適配器只帶或不帶參數運行的更新語句。 這個替換您的代碼(objDataAdapter.UpdateCommand = objUpdateCommand):

Dim UpdateResultValue as Integer 

UpdateResultValue = objUpdateCommand.ExecuteNonQuery 

If UpdateResultValue = 0 Then 
    msgbox "Table Update Failed" 
Else 
    msgbox "Table Updated" 
End if 

如果返回一個零,它不工作(UpdateResultValue = 0)。當您不從數據庫表中返回值時可能會使用ExecuteNonQuery(可能過於簡化)。

0

您的參數需要與查詢中使用的順序相同。

+0

我的參數與查詢中的順序相同。我的查詢是:Const SQLExpression As String =「UPDATE product SET Value = @pValue where owner = @powner and Item = @pItem;」我的訂單是:使用objUpdateCommand .Parameters.Add(「@ pValue」,OleDbType.VarChar,10,「Value」).Parameters.Add(「@ powner」,OleDbType.VarChar,10,「owner」).Parameters。添加(「@ pItem」,OleDbType.VarChar,8,「Item」)End With – user688175 2011-04-04 13:16:49

+0

現在得到的錯誤是:參數@Value沒有默認值 – user688175 2011-04-04 13:19:13

+0

將命令類型設置爲文本'objUpdateCommand.CommandType = CommandType.Text'取而代之'objDataAdapter.UpdateCommand = objUpdateCommand'使用'objUpdateCommand.ExecuteScalar()'。同時逐行瀏覽您的代碼並確保參數具有您期望的值。 – Jack 2011-04-05 14:14:02