update a database, using merge and getChanges
Hallo,
I need some help. I want/ need to write a little program to update an database. Structure update and data update. I want to use VB.NET (and Ado.NET).
There are two databases - a source and a target. At the end, they should have the same structure and content. I want to try it first with one table, which exists and has the same name in both databases. (Connection, provider - this works... - so I don’t have it in the code example... ) I have two datasets, fill it, and think the best way is to use the "merge-method" and then I proof "HasChanges". If there are Changes on the Database, I want to use the "getChanges"-method. But this doesn’t work. What ever I try, after the "
If myDS1.HasChanges()" question the program always jumps to "endif". I tried different variant’s (for instance: "myDS1.Merge(myDS2, False, MissingSchemaAction.Add)"),
but always the same result. Think I have a problem in my logic.
If somebody could give me an advice/ help or tell me where my mistake is ... .
thanks
Here is one version of what I tried:
Imports System.Data
Imports System.Data.OleDb
Module Module1
Sub Main()
Dim myDS1 As New DataSet()
Dim myTable1 As New DataTable("TPPerson") ‘ here I’m not sure
myDS1.Tables.Add(myTable1)
Dim strSQL1 = "Select * from TPPerson"
adp1 = New OleDbDataAdapter(strSQL1, cnn1)
adp1.Fill(myDS1, "TPPerson")
Dim myDS2 As New DataSet()
Dim myTable2 As New DataTable("TPPerson")
myDS2.Tables.Add(myTable2)
Dim strSQL2 = "Select * from TPPerson"
adp2 = New OleDbDataAdapter(strSQL2, cnn2)
adp2.Fill(myDS2, "TPPerson")
myDS1.Merge(myTable2, False, MissingSchemaAction.Add)
‘ here could be a mistake
myDS1.AcceptChanges()
If myDS1.HasChanges() _
Then
Dim myDS3 As New DataSet()
myDS3 = myDS1.GetChanges(DataRowState.Modified)
myDS1.Merge(myDS3, False, System.Data.MissingSchemaAction.Add)
adp1.Update(myDS1, "TPPerson")
End If