我刚刚发现了CommandBuilder,并认为它听起来很直接且易于使用。显然,我仍然缺少一些东西。我有一个成功更新名为Customers的dbSet表的DataGridView。但它没有更新到实际的数据库文件:
...
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
它不会抛出错误,我所做的任何更改都会保留在内存中(我可以打开和关闭表单并且更改将保留),但它们实际上并未写入D B。我错过了什么?
答案 0 :(得分: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