vb.net 无效操作异常未处理更新需要有效的更新命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15446466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
invalid operation exception was unhandled Update requires a valid UpdateCommand
提问by larca
From these codes, I want to edit, add and save data from VB to MS Access permanently. I created dozens of Visual Basic projects but no progress at all.
从这些代码中,我想从 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
The problem is that "invalid operation exception was unhandled" appears, Update requires a valid UpdateCommandfrom code Me.TableAdapterManager.UpdateAll(Me.INVSYSDataSet)
问题是出现“invalid operation exception was unhandled”,Update需要一个valid UpdateCommandfrom codeMe.TableAdapterManager.UpdateAll(Me.INVSYSDataSet)
If you need the DataSource, I can provide code from another VB project.
如果您需要数据源,我可以提供来自另一个 VB 项目的代码。
*updated second code, help for sql please *updated srry bout that
*更新了第二个代码,请帮助sql *更新srry回合
Public Class Add_Products
公共类 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
End Class
结束班
回答by Tim
The error message is telling you that you have not defined an Update command for the DataAdapter. From DbDataAdapter.Update Method DataSet, String: If INSERT, UPDATE, or DELETE statements have not been specified, the Update method generates an exception.
该错误消息告诉您尚未为 DataAdapter 定义更新命令。从DbDataAdapter.Update 方法数据集,字符串:If INSERT, UPDATE, or DELETE statements have not been specified, the Update method generates an exception.
To resolve this, assign the UpdateCommandan OleDbCommandobject with your update logic, like this:
要解决此问题,请使用更新逻辑分配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
结束子
Simply put your SQL in the OleDbCommandand assign it to the UpdateCommandproperty.
只需将您的 SQL 放入OleDbCommand并将其分配给UpdateCommand属性。
Look at this link for a detailed example (and be sure to use parameterized queries like in the example to avoid SQL Injection attacks): OleDbDataAdapter.UpdateCommand Property
查看此链接以获取详细示例(并确保使用示例中的参数化查询以避免 SQL 注入攻击):OleDbDataAdapter.UpdateCommand 属性

