Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用datagrid中的值更新访问_Vb.net_Ms Access - Fatal编程技术网

Vb.net 使用datagrid中的值更新访问

Vb.net 使用datagrid中的值更新访问,vb.net,ms-access,Vb.net,Ms Access,无法使用下面的函数更新access中的表 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 OleDbDataAd

无法使用下面的函数更新access中的表

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

设置updateCommand属性不会执行该命令。您需要调用表适配器的update方法


查看更多信息

运行update语句时,不必使用适配器,也可以使用或不使用参数。 将代码(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)。当您不从数据库表返回值时,将使用ExecuteOnQuery(可能过于简化)。

您的参数需要与查询中使用的参数顺序相同。

我的参数与查询中的参数顺序相同。。我的查询是:Const SQLExpression As String=“UPDATE product SET Value=@pValue,其中owner=@powner,Item=@pItem”;我的顺序是:With objUpdateCommand.Parameters.Add(“@pValue”,OleDbType.VarChar,10,“Value”)。Parameters.Add(“@powner”,OleDbType.VarChar,10,“owner”)。Parameters.Add(“@pItem”,OleDbType.VarChar,8,“Item”)我现在得到的结果是:参数@Value没有默认值将命令类型设置为Text
objUpdateCommand.CommandType=CommandType.Text
而不是
objDataAdapter.UpdateCommand=objUpdateCommand
使用
objUpdateCommand.ExecuteScalar()
。还要逐行检查代码,确保参数具有您期望的值。