在使用新行传递DataRow集合时,更新记录需要有效的InsertCommand

时间:2015-10-14 21:50:43

标签: vb.net ms-access

我正在尝试将表单中的数据添加回Access表中,但我不断收到此错误消息“在使用新行传递DataRow集合时,Update需要有效的InsertCommand”。而对于我的生活,我无法解决我需要做的事情。

这是按钮点击的代码,它应该更新记录。

公共课程订单

Dim ClientOrderConnection As New OleDb.OleDbConnection

Dim Provider As String
Dim dbSource As String
Dim sqlQuery As String

Dim dsClientOrder As New DataSet
Dim daClientOrder As New OleDb.OleDbDataAdapter
Dim dtOrders As New Data.DataTable
Dim Booking As New ArrayList



Dim RowNumber As Integer
Dim Counter As Integer = 0
Dim NumberOfRows As Integer

Private Sub Orders_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)处理MyBase.Load

    Provider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
    dbSource = "Data Source = A2ComputingDatabase.accdb"

    ClientOrderConnection.ConnectionString = Provider & dbSource

    ClientOrderConnection.Open()

    sqlQuery = "SELECT * FROM TblClientOrder"

    daClientOrder = New OleDb.OleDbDataAdapter(sqlQuery, ClientOrderConnection)

    daClientOrder.Fill(dsClientOrder, "ClientOrder")

    ClientOrderConnection.Close()

    NumberOfRows = dsClientOrder.Tables("ClientOrder").Rows.Count

Private Sub btnSubmit_Click(sender As System.Object,e As System.EventArgs)处理btnSubmit.Click

    If RowNumber <> -1 Then

        Dim cbClientOrder As New OleDb.OleDbCommandBuilder
        Dim dsClientNewRow As DataRow




        dsClientNewRow = dsClientOrder.Tables("ClientOrder").NewRow()

        dsClientNewRow.Item("ClientOrderNumber") = txtOrderNo.Text
        dsClientNewRow.Item("ClientTelNo") = txtClientTelNo.Text

        dsClientOrder.Tables("ClientOrder").Rows.Add(dsClientNewRow)

        daClientOrder.Update(dsClientOrder, "ClientOrder")


        MsgBox("New Reocrd added to the Database")
    End If



End Sub

非常感谢任何帮助,提前谢谢!

1 个答案:

答案 0 :(得分:0)

您实际上必须从DataAdapter实例化OledbCommandBuilder:

cbClientOrder = New OleDb.OleDbCommandBuilder(daClientOrder)

使用您的DataAdapter对cbClientOrder进行Dim然后将其放在daClientOrder.Fill()调用之后的某处,您应该很高兴。当然,在你的btnSubmit_Click事件中摆脱cbClientOrder的其他声明。