在 Visual Basic 中更新表适配器时出错

发布于 2024-12-08 01:32:45 字数 303 浏览 0 评论 0原文

我正在尝试通过使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里兽 2024-12-15 01:32:45

您提到的错误消息与您尝试更新的表上的主键无关,而是在抱怨,因为您没有为适配器提供可用于更新基础表的有效命令。

如果您尝试插入或删除新行而未在适配器上指定插入或删除命令,则会遇到相同的错误。

要解决此问题,请将 UpdateCommand 属性初始化为有意义的值,例如:

'Open connection
Using Conn as new OleDbConnection("connection string")

  'This is the command that will update your table in the database
  Using UpdateCmd = Conn.CreateCommand()

    UpdateCmd.CommandText = "update yourtable set col1 = ?, col2 = ?"

    'Create a parameter to pass in the value of the "col1" column on "yourtable"
    Dim ColOneParam = UpdateCmd.Parameters.Add("@Col1", OleDbType.Integer)
    ColOneParam.SourceColumn = "Name of column in DataTable which corresponds to col1"

    'Create a parameter to pass in the value of the "col2" column on "yourtable"
    Dim ColTwoParam = UpdateCmd.Parameters.Add("@Col2", OleDbType.Integer)
    ColTwoParam.SourceColumn = "Name of column in DataTable which corresponds to col2"

    'Data adapter which will perform the specified update command for each 
    'newly inserted row
    Using Adapter As New OleDbDataAdapter

      'Set the update command on the adapter, if you omit this line you'll
      'encounter the original error you mentioned
      Adapter.UpdateCommand = UpdateCmd

      'This is the data table containing the rows you wish to update
      Dim NewRows As New DataTable("SomeTable")

      'For each modified row in NewRows, update it in the database
      Adapter.Update(NewRows)

    End Using

  End Using

End Using

上面的代码假设数据库中有一个名为 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:

'Open connection
Using Conn as new OleDbConnection("connection string")

  'This is the command that will update your table in the database
  Using UpdateCmd = Conn.CreateCommand()

    UpdateCmd.CommandText = "update yourtable set col1 = ?, col2 = ?"

    'Create a parameter to pass in the value of the "col1" column on "yourtable"
    Dim ColOneParam = UpdateCmd.Parameters.Add("@Col1", OleDbType.Integer)
    ColOneParam.SourceColumn = "Name of column in DataTable which corresponds to col1"

    'Create a parameter to pass in the value of the "col2" column on "yourtable"
    Dim ColTwoParam = UpdateCmd.Parameters.Add("@Col2", OleDbType.Integer)
    ColTwoParam.SourceColumn = "Name of column in DataTable which corresponds to col2"

    'Data adapter which will perform the specified update command for each 
    'newly inserted row
    Using Adapter As New OleDbDataAdapter

      'Set the update command on the adapter, if you omit this line you'll
      'encounter the original error you mentioned
      Adapter.UpdateCommand = UpdateCmd

      'This is the data table containing the rows you wish to update
      Dim NewRows As New DataTable("SomeTable")

      'For each modified row in NewRows, update it in the database
      Adapter.Update(NewRows)

    End Using

  End Using

End Using

The above code assumes a table in the database called yourtable with two columns col1 and col2 which are numeric.

The Using blocks simply ensure that the database objects are properly disposed off so you don't end up leaking resources.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文