W
WATYF1
I honestly didn't think this would be such an impossible task, but it
has proven to be so. All I am trying to do is copy data from a table in
one database to an indentical table in another database. I want to do
this in order to backup my main database periodically. So, the main db
has data in it, and the bak database has empty tables (which are the
same tables/schema as the main db).
At this point, using information that I have painfully deciphered from
the vague and not-so-helpful posts I've run across so far, I was able
to hack together the following code. Basically, my approach is... fill
a datatable from one database, and update that datatable into the other
database. Unfortunately, it doesn't work... I keep getting an error.
But first, here's the code:
Dim tbls() As String = New String() {"MyTable1", "MyTable2",
"MyTable3", "MyTable4"}
For i As Integer = 0 To UBound(tbls)
'Fill datatable from main db
da = New DataAdapter("SELECT * FROM " & tbls(i), con)
da.AcceptChangesDuringFill = False
dt = New DataTable
da.Fill(dt)
da.Dispose()
If Not dt Is Nothing Then
'Create da to bak db and update it using the dt from
main db
da = New DataAdapter("SELECT * FROM " & tbls(i), bak)
da.Update(dt)
da.Dispose()
dt.Dispose()
End If
Next
The error occurs on the "da.Update(dt) line... and here's the error:
"Update requires a valid InsertCommand when passed DataRow collection
with new rows."
I've seen numerous *mentions* of using an InsertCommand to perform
operations such as this, but it seems that no one thought to actually
post any samples of HOW to do it, so I'm at a loss as to how to get
around this error.
What's troubling is, I've seen a number of posts that ask this (or a
similar) question, but I have yet to see one comprehensive solution
offered. I guess I'm just hoping to be the exception. p
WATYF
has proven to be so. All I am trying to do is copy data from a table in
one database to an indentical table in another database. I want to do
this in order to backup my main database periodically. So, the main db
has data in it, and the bak database has empty tables (which are the
same tables/schema as the main db).
At this point, using information that I have painfully deciphered from
the vague and not-so-helpful posts I've run across so far, I was able
to hack together the following code. Basically, my approach is... fill
a datatable from one database, and update that datatable into the other
database. Unfortunately, it doesn't work... I keep getting an error.
But first, here's the code:
Dim tbls() As String = New String() {"MyTable1", "MyTable2",
"MyTable3", "MyTable4"}
For i As Integer = 0 To UBound(tbls)
'Fill datatable from main db
da = New DataAdapter("SELECT * FROM " & tbls(i), con)
da.AcceptChangesDuringFill = False
dt = New DataTable
da.Fill(dt)
da.Dispose()
If Not dt Is Nothing Then
'Create da to bak db and update it using the dt from
main db
da = New DataAdapter("SELECT * FROM " & tbls(i), bak)
da.Update(dt)
da.Dispose()
dt.Dispose()
End If
Next
The error occurs on the "da.Update(dt) line... and here's the error:
"Update requires a valid InsertCommand when passed DataRow collection
with new rows."
I've seen numerous *mentions* of using an InsertCommand to perform
operations such as this, but it seems that no one thought to actually
post any samples of HOW to do it, so I'm at a loss as to how to get
around this error.
What's troubling is, I've seen a number of posts that ask this (or a
similar) question, but I have yet to see one comprehensive solution
offered. I guess I'm just hoping to be the exception. p
WATYF