#Region "Make a Table to a Database"
Public Sub FillTable(ByVal sNewTable As String, ByVal sOldTable As String, ByVal sDBName As String)
sSql = "INSERT INTO " & sNewTable & " IN " & sDBName & " SELECT FieldName1, FieldName2, FieldName3, FieldName4, FieldName5 " & _
"FROM " & sOldTable & " IN " & sDBName
RunSQL(sSql)
End Sub
Private Sub RunSQL(ByVal strSQL As String)
Dim Cmd1 = New OleDb.OleDbCommand(strSQL, Con)
Try
Cmd1.Connection.Open()
Cmd1.ExecuteNonQuery()
Cmd1.Connection.Close()
Catch exp As OleDb.OleDbException
Dim errorMessages As String
Dim i As Integer
For i = 0 To exp.Errors.Count - 1
errorMessages += "Index #" & i.ToString() & ControlChars.Cr _
& "Message: " & exp.Errors(i).Message & ControlChars.Cr _
& "NativeError: " & exp.Errors(i).NativeError & ControlChars.Cr _
& "Source: " & exp.Errors(i).Source & ControlChars.Cr _
& "SQLState: " & exp.Errors(i).SQLState & ControlChars.Cr
Next i
Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog
log.Source = "My Application"
log.WriteEntry(errorMessages)
MessageBox.Show("An exception occurred. Please contact your system administrator.")
Catch exp As Exception
Cmd1.Connection.Close()
'' MessageBox.Show(exp.Message, MsgBoxStyle.Critical, "General Error")
End Try
End Sub
#End Region
#Region "Copy a Table"
'USE CopyTable("D:\athos.mdb", "client", "clienttest")
Public Sub CopyTable(ByVal DBPathName As String, ByVal TargetTable As String, ByVal NewTableName As String)
Dim cmd1 As New OleDb.OleDbCommand("SELECT " & TargetTable & ".* INTO " & NewTableName & " IN '" & DBPathName & "' From " & TargetTable)
cmd1.Connection = Con
Con.Open()
Try
cmd1.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
#End Region
Private Sub AccessToAccess(ByVal sourceFile As String, ByVal sourceTable As String, ByVal targetFile As String, ByVal targetTable As String)
'Sub transfers all records from ..mdb sourcefile.sourcetable ...
'... to .mdb targetfile.targettable
'It is assumed that the .mdb targettable definition already ...
'... exists, with the same number and types of fields, ...
'... in the same order, as the .mdb sourcetable fields.
'It does not depend on the field names being the same ...
'... in the source and target tables.
Dim sourceConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sourceFile & ";User Id=admin;Password=;"
Dim sourceSQL As String = "SELECT * FROM " & sourceTable
Dim targetConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & targetFile & ";User Id=admin;Password=;"
Dim targetSQL As String = "SELECT * FROM " & targetTable
'use dataadapter for target and command builder to ...
'... create insert command, including parameter collection
Dim targetCon As New OleDbConnection(targetConStr)
Dim targetDA As New OleDbDataAdapter(targetSQL, targetCon)
Dim cb As New OleDbCommandBuilder(targetDA)
Dim targetCommand As OleDbCommand = cb.GetInsertCommand
'now do the work
Dim sourceCon As New OleDbConnection(sourceConStr)
Dim sourceCommand As New OleDbCommand(sourceSQL, sourceCon)
targetCommand.Connection.Open()
sourceCon.Open()
Dim sourceReader As OleDbDataReader
sourceReader = sourceCommand.ExecuteReader()
While sourceReader.Read()
'for each row from source
For i As Integer = 0 To sourceReader.FieldCount - 1
'load values into parameters
targetCommand.Parameters(i).Value = sourceReader(i)
'if any of the source data needs to be modified ...
'... for the target, it could be done here ...
Next
'... or here
'then write to target
targetCommand.ExecuteNonQuery()
End While
sourceReader.Close()
sourceCon.Close()
targetCommand.Connection.Close()
End Sub
'Ron used 2 access databases db1, db2 as you can see in the connection
' strings with identical tables called tabel1 in db1 and tabel2 in db2,
' Ron added to oledbdataadapters (da1,da2)
Dim con1 As String = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database L" & _
"ocking Mode=1;Jet OLEDB:Database Password=;Data Source=""C:\" & _
"db1.mdb"";Password=;Jet OLEDB:Engine Type=5;Jet" & _
" OLEDB:Global Bulk Transactions=1;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:S" & _
"ystem database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Je" & _
"t OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:" & _
"Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=Fals" & _
"e;User ID=Admin;Jet OLEDB:Encrypt Database=False"
Dim con2 As String = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database L" & _
"ocking Mode=1;Jet OLEDB:Database Password=;Data Source=""C:\" & _
"db2.mdb"";Password=;Jet OLEDB:Engine Type=5;Jet" & _
" OLEDB:Global Bulk Transactions=1;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:S" & _
"ystem database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Je" & _
"t OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:" & _
"Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=Fals" & _
"e;User ID=Admin;Jet OLEDB:Encrypt Database=False"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ds As New DataSet
Dim da1 As New OleDb.OleDbDataAdapter("select * from tabel1", con1)
Dim da2 As New OleDb.OleDbDataAdapter("select * from tabel2", con2)
da1.Fill(ds)
Dim db As New OleDb.OleDbCommandBuilder(da2)
da2.Update(ds.Tables(0))
End Sub
Dim con1 As String = _
"Jet OLEDB:Global Partial Bulk Ops=2;" & _
"Jet OLEDB:Registry Path=;" & _
"Jet OLEDB:Database Locking Mode=1;" & _
"Jet OLEDB:Database Password=;" & _
"Data Source=C:\temp.mdb;" & _
"Password=;" & _
"Jet OLEDB:Engine Type=5;" & _
"Jet OLEDB:Global Partial Bulk Ops=2;" & _
"Jet OLEDB:Global Bulk Transactions=1;" & _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Jet OLEDB:System database=;" & _
"Jet OLEDB:SFP=False;" & _
"Extended Properties=;" & _
"Locale Identifier=1033;" & _
"Mode=Share Deny None;" & _
"Jet OLEDB:New Database Password=;" & _
"Jet OLEDB:Create System Database=False;" & _
"Jet OLEDB:Don't Copy Locale on Compact=False;" & _
"Jet OLEDB:Compact Without Replica Repair=False;" & _
"User ID=Admin;" & _
"Jet OLEDB:Encrypt Database=False"
Dim con2 As String = _
"Jet OLEDB:Global Partial Bulk Ops=2;" & _
"Jet OLEDB:Registry Path=;" & _
"Jet OLEDB:Database Locking Mode=1;" & _
"Jet OLEDB:Database Password=;" & _
"Data Source=C:\temp2.mdb;" & _
"Password=;" & _
"Jet OLEDB:Engine Type=5;" & _
"Jet OLEDB:Global Partial Bulk Ops=2;" & _
"Jet OLEDB:Global Bulk Transactions=1;" & _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Jet OLEDB:System database=;" & _
"Jet OLEDB:SFP=False;" & _
"Extended Properties=;" & _
"Locale Identifier=1033;" & _
"Mode=Share Deny None;" & _
"Jet OLEDB:New Database Password=;" & _
"Jet OLEDB:Create System Database=False;" & _
"Jet OLEDB:Don't Copy Locale on Compact=False;" & _
"Jet OLEDB:Compact Without Replica Repair=False;" & _
"User ID=Admin;" & _
"Jet OLEDB:Encrypt Database=False"
Private Sub Move_datsets()
Dim ds As New DataSet
Dim ds2 As New DataSet
Dim da1 As New OleDb.OleDbDataAdapter("select * from table1", con1)
Dim da2 As New OleDb.OleDbDataAdapter("select * from table1", con2)
da1.AcceptChangesDuringFill = False
da1.Fill(ds)
ds2.Merge(ds)
Dim db As New OleDb.OleDbCommandBuilder(da2)
db.QuotePrefix = "["
db.QuoteSuffix = "]"
da2.Update(ds2.Tables(0))
End Sub