2011-04-01 59 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