0

我刚刚发现了 CommandBuilder,并认为它听起来很直接且易于使用。显然我仍然缺少一些东西。我有一个 DataGridView 可以成功更新名为 Customers 的 dbSet 表。但它没有更新到实际的数据库文件:

...
Dim ConMain As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\dummy_data.accdb")
...
Private Sub CustomerDataGridView_RowValidated(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
    Handles CustomerDataGridView.RowValidated

    Me.CustomersTableAdapter.Update(Me.Dummy_dataDataSet.Customers)
    Dim CustomerAdapter As New OleDbDataAdapter("Select * From Customers", ConMain)
    Dim ObjComander As New OleDbCommandBuilder(CustomerAdapter)
    CustomerAdapter.Update(Dummy_dataDataSet, "Customers")

End Sub

它不会抛出错误,并且我所做的任何更改都保存在内存中(我可以打开和关闭表单并且更改将保留),但它们实际上并未写入数据库。我错过了什么?

4

1 回答 1

0

尝试这个

Dim ConMain As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\dummy_data.accdb")
Dim m_DtCustomer As New DataTable
Dim CustomerAdapter As OleDbDataAdapter
Dim m_Bsource As New BindingSource

'Populate the datagridview

Sub FillDataGrid()

    CustomerAdapter = New OleDbDataAdapter("Select * From Customers", ConMain)
    m_DtCustomer.Clear()
    CustomerAdapter.Fill(m_DtCustomer)
    m_Bs.DataSource = m_DtCustomer
    CustomerDataGridView.Datasource = m_Bs
End Sub

'Update changes to the database

Sub UpdateDatabase()

    Dim ObjComander As New OleDbCommandBuilder(CustomerAdapter)
    CustomerAdapter.Update(m_DtCustomer)

End Sub

Private Sub CustomerDataGridView_RowValidated(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles CustomerDataGridView.RowValidated

    Call Me.UpdateDatabase()

End Sub
于 2013-03-19T09:24:06.447 回答