无效操作异常未处理更新需要有效的UpdateCommand

时间:2013-03-16 06:53:32

标签: vb.net ms-access invalidoperationexception

从这些代码中,我想永久地编辑,添加和保存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

问题是“无法处理无效操作异常”,更新需要代码UpdateCommand中的有效Me.TableAdapterManager.UpdateAll(Me.INVSYSDataSet)

如果您需要DataSource,我可以提供其他VB项目的代码。

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

公共类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

结束班

1 个答案:

答案 0 :(得分: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

End Sub

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

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