使用DataAdapter从DataGridView更新数据库表

时间:2019-05-15 13:28:03

标签: vb.net oracle

编辑:通过直接查询表而不是通过使用存储过程和ref游标来解决。

我有一个仅包含DataGridView dgvDetail的表单。这是表单类代码:

Imports Oracle.ManagedDataAccess.Client

Public Class FRM_EditDetail

    Dim DataAdapter As New OracleDataAdapter
    Dim CommandBuilder As New OracleCommandBuilder(DataAdapter)
    Dim Connection As New OracleConnection(connectionString)
    Dim Command As OracleCommand = Connection.CreateCommand()

    Private Sub FRM_EditDetail_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Connection.Open()
        Command.CommandType = CommandType.StoredProcedure
        Command.CommandText = "SV_PACKAGE.GetDetail"
        Command.Parameters.Add("P_CURSOR", OracleDbType.RefCursor).Direction = ParameterDirection.Output
        DataAdapter.SelectCommand = Command
        DataAdapter.Fill(dsData, "Detail")
        dsData.Tables("Detail").PrimaryKey = New DataColumn() {dsData.Tables("Detail").Columns("ID")}
        dgvNHHDetail.DataSource = dsData.Tables("Detail")

    End Sub

    Private Sub FRM_EditDetail_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing

        CommandBuilder.GetUpdateCommand()
        DataAdapter.Update(dsData, "Detail")
        Connection.Close()

    End Sub

End Class

我希望用户打开表单,更改数据,并在他们关闭表单时将更改传递到数据库表中。

只要从数据库表中正确填充了LoaddgvDetail事件就可以正常工作。我在dsData.Tables("Detail").PrimaryKey = New DataColumn() {dsData.Tables("Detail").Columns("ID")}行上没有任何错误,并且我的数据库表的主键位于同一列。

但是FormClosing事件触发时,获得Dynamic SQL generation failed. No key information found

分配主键(或其他东西)时我怎么了?

0 个答案:

没有答案