Search code examples
vb.netdatatableoledb

Easiest way to update a OleDB table from a DataTable


What's the easiest way (vb.NET) to update a full OleDB table from a previous modified imported table?

Here's what I have.

First I import the table from an Access Database:

Dim PRDB As String = "Provider=Microsoft.ACE.OLEDB.12.0;                                    
                      Data Source=<location>;
                      Jet OLEDB:Database Password=<password>"                                                        
Dim CNDB As New OleDb.OleDbConnection(PRDB)                                                                              
Dim CMDB As New OleDb.OleDbCommand                                                                                
Dim ADDB As New OleDb.OleDbDataAdapter(CMDB)                                                                       
Dim TBDB As New DataTable                                                                                                    

Try : CNDB.Open()
Catch EX As Exception : MsgBox(EX.ToString) : End Try

Try : CMDB.CommandText = "SELECT * FROM [TABLE 01]" : ADDB.Fill(TBDB)
Catch EX As Exception : MsgBox(EX.ToString) : End Try

So far so good. I managed to import TABLE 01 successfully to TBDB.

Now, let's say I change one cell of the DataTable:

TBDB.Rows(2).Item(3) = "CHANGED"

Is there a way to update directly the OleBD table without describing the changed cell adress? Something like

ADDB.Update(TBDB) (?)

I already tried the last bit of code but it didn't work (I think it works only if the argument is a DataRow not a DataTable).

I researched this matter, but all the answers I found looked a lit bit complicated for this (apparent) simple task. So I wonder if there is a more direct way to do this - I also read something about Binders, but I didn't quite understand the method.

Thanks


Solution

  • Thanks to @Fadi, I managed to get the exact method I was looking for using the OleDbCommandBuilder:

    Dim CMNDBUID As OleDb.OleDbCommandBuilder
    CMNDBUID = New OleDb.OleDbCommandBuilder(ADDB)
    CMNDBUID.GetUpdateCommand()
    

    Then I can easily update the Access table:

    Try : ADDB.Update(TBDB)
    Catch EX As Exception : MsgBox(EX.ToString) : End Try