在 Visual Basic 中更新表适配器时出错
我正在尝试通过使用 Visual Basic 形式进行编码来提高我的编程能力。我创建了一个数据库并将其链接到 VB 表单,现在我正在编写一种写入数据库而不是读取的方法。
我正在填充一个数组,该数组又逐行放入数据集中,但是当尝试“更新”表适配器时,我收到以下错误:
当传递包含修改行的 DataRow 集合时,更新需要有效的 UpdateCommand。
我相信这与主键有关,我的适配器正在更新的表没有主键,因为它涉及与另一个包含主键的表的关系。如何更新我的表适配器而不出现此错误?谢谢。
I'm trying to progress my programming abilities by coding in Visual Basic forms. I've created a database and linked that to a VB form, I'm now coding a way to write into the database instead of reading.
I'm filling an array which is in-turn put into the dataset row by row however when attempting to 'update' the table adapter I get the following error:
Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
I believe this has something to do with primary keys, the table that my adapter is updating does NOT have a primary key as it's involved in a relationship with another table, containing a primary key. How can I update my table adapter without getting this error? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提到的错误消息与您尝试更新的表上的主键无关,而是在抱怨,因为您没有为适配器提供可用于更新基础表的有效命令。
如果您尝试插入或删除新行而未在适配器上指定插入或删除命令,则会遇到相同的错误。
要解决此问题,请将
UpdateCommand
属性初始化为有意义的值,例如:上面的代码假设数据库中有一个名为
yourtable
的表,其中包含两列col1 和
col2
是数字。Using
块只是确保数据库对象得到正确处理,这样您就不会泄漏资源。The error message you've mentioned has nothing to do with primary keys on the table you are attempting to update, instead it's complaining because you haven't given your adapter a valid command which it can use to update the underlying table.
You'd hit the same error if you attempted to insert or delete new rows without specifying an insert or delete command on your adapter.
To solve the problem, initialise your
UpdateCommand
property to something meaningful, for example:The above code assumes a table in the database called
yourtable
with two columnscol1
andcol2
which are numeric.The
Using
blocks simply ensure that the database objects are properly disposed off so you don't end up leaking resources.