【问题标题】:invalid operation exception was unhandled Update requires a valid UpdateCommand未处理无效操作异常 更新需要有效的 UpdateCommand
【发布时间】:2013-03-16 06:53:32
【问题描述】:

通过这些代码,我想从 VB 编辑、添加数据并将其永久保存到 MS Access。我创建了几十个 Visual Basic 项目,但毫无进展。

Public Class Form1

    Private Sub ProductDescBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProductDescBindingNavigatorSaveItem.Click

        Me.Validate()
        Me.ProductDescBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.INVSYSDataSet)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'TODO: This line of code loads data into the 'INVSYSDataSet.ProductDesc' table. You can move, or remove it, as needed.
        Me.ProductDescTableAdapter.Fill(Me.INVSYSDataSet.ProductDesc)
    End Sub
End Class

问题是出现“invalid operation exception was unhandled”,更新需要来自代码Me.TableAdapterManager.UpdateAll(Me.INVSYSDataSet)的有效UpdateCommand

如果您需要数据源,我可以提供另一个 VB 项目的代码。

*更新了第二个代码,请帮助sql *更新了 srry 回合

公共类 Add_Products

Private myConString As String
Private con As OleDb.OleDbConnection = New OleDb.OleDbConnection
Private Dadapter As OleDb.OleDbDataAdapter
Private DSet As DataSet
Private DSet2 As DataSet
Private ConCMD As OleDb.OleDbCommand
Dim strSql As String
Dim inc As Integer
Dim MaxRows As Integer

Private Sub Add_Products_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    myConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\larca\Documents\Visual Studio 2010\Projects\march16\march16\obj\x86\Debug\INVSYS.mdb"
    con.ConnectionString = myConString
    con.Open()
    Dadapter = New OleDb.OleDbDataAdapter("select * from ProductDesc", con)
    DSet = New DataSet
    Dadapter.Fill(DSet, "ProductDesc")
    DataGridView1.DataSource = DSet.Tables("ProductDesc")

    con.Close()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        Using con = New OleDb.OleDbConnection(myConString)
            con.Open()
            Dim cmd As OleDb.OleDbCommand
            cmd = New OleDb.OleDbCommand("UPDATE ProductDesc", con)
            Dadapter.UpdateCommand = cmd
            Dadapter.Update(DSet, "ProductDesc")
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

结束类

【问题讨论】:

  • 您是否为 DataAdapter 定义了更新命令? OleDbDataAdapter.UpdateCommand Property
  • 你是什么意思?创建命令?
  • 您需要为 DataAdapter 提供更新命令的 SQL。这就是错误消息告诉您的内容。看看我在之前评论中提供的链接。
  • 我在哪里可以定义它?
  • 打开 MS Access,粘贴更新中的 sql。你会发现它不起作用。您必须使用有效的 sql。

标签: vb.net ms-access invalidoperationexception


【解决方案1】:

错误消息告诉您尚未为 DataAdapter 定义更新命令。来自DbDataAdapter.Update Method DataSet, StringIf INSERT, UPDATE, or DELETE statements have not been specified, the Update method generates an exception.

要解决此问题,请使用您的更新逻辑为 UpdateCommand 分配一个 OleDbCommand 对象,如下所示:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

   Using con = New OleDb.OleDbConnection(myConString)
       con.Open()
       Dim cmd As OleDbCommand
       cmd = New OleDbCommand("<your update SQL goes here>", con)
       DAdapter.UpdateCommand = cmd 
       Dadapter.Update(DSet, "ProductDesc")
   End Using

结束子

只需将您的 SQL 放入 OleDbCommand 并将其分配给 UpdateCommand 属性即可。

查看此链接以获取详细示例(并确保使用示例中的参数化查询以避免 SQL 注入攻击):OleDbDataAdapter.UpdateCommand Property

【讨论】:

  • 我做不到,你能帮帮我吗
  • 不能做什么?让代码工作?写SQL更新?发布您尝试过的内容(编辑您的问题,使其格式正确)。
  • 这里我编辑了第二个代码,你能帮帮我吗,在我的系统分析项目中卡住了 1 天
  • 无法参数化,因为我在参数化 gridview 时很困惑,我还不知道参数化
  • 您的 SQL 命令不是更新 - 它是一个简单的 SELECT。更新类似于 UPDATE tablename SET columnname = value WHERE someothercolumname = someothervalue。
猜你喜欢
  • 2014-01-12
  • 2018-03-30
  • 2016-10-11
  • 1970-01-01
  • 1970-01-01
  • 2010-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多