使用VB 6和Access 2003如何將表從一個數據庫複製到其他數據庫?
我想將表從一個數據庫複製到其他數據庫。
數據庫1
Table1
Table2
的Database2
Table3
上面,我想在表3複製到數據庫1
Expected Output
Table1
Table2
Table3
如何寫代碼?
需要VB6代碼幫助。
使用VB 6和Access 2003如何將表從一個數據庫複製到其他數據庫?
我想將表從一個數據庫複製到其他數據庫。
數據庫1
Table1
Table2
的Database2
Table3
上面,我想在表3複製到數據庫1
Expected Output
Table1
Table2
Table3
如何寫代碼?
需要VB6代碼幫助。
使用ADOX複製數據結構可能是最簡單的方法。
Dim sourceCat As New ADOX.Catalog
Dim targetCat As New ADOX.Catalog
Set sourceCat.ActiveConnection = connSource
targetCat.ActiveConnection = connTarget
Dim sourceTable As ADOX.Table
Set sourceTable = sourceCat.Tables("TableName")
Dim newTable As New ADOX.Table
Set newTable.ParentCatalog = targetCat
newTable.Name = sourceTable.Name
Dim sourceCol As ADOX.Column
Dim newCol As ADOX.Column
For Each sourceCol In sourceTable.Columns
Set newCol = New ADOX.Column
newCol.Name = sourceCol.Name
newCol.Type = sourceCol.Type
newCol.DefinedSize = sourceCol.DefinedSize
newCol.ParentCatalog = targetCat
newTable.Columns.Append newCol
Next sourceCol
targetCat.Tables.Append newTable
這是一個相當基本的例子,它忽略所有索引 和列的屬性(如自動增量)。
可以找到一個更完整的例子here。
請注意,您必須添加對ADOX的引用。 – 2009-09-02 19:01:08
請注意,即使在同時使用ADO(您需要的約束條件,WITH COMPRESSION
等)和ACEDAO(您需要複雜的數據類型等)時,您也無法確定是否已提取了表的所有模式。
這裏是這樣的表的一個例子:
Sub CantGetCheck()
On Error Resume Next
Kill Environ$("temp") & "\DropMe.mdb"
On Error GoTo 0
Dim cat
Set cat = CreateObject("ADOX.Catalog")
With cat
.Create _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
Environ$("temp") & "\DropMe.mdb"
With .ActiveConnection
Dim Sql As String
Sql = _
"CREATE TABLE Test " & _
"(" & _
" data_col INTEGER NOT NULL, " & _
" CONSTRAINT data_col__be_positive " & _
" CHECK (data_col >= 0), " & _
" CONSTRAINT data_col__values " & _
" CHECK (" & _
" data_col = 0 OR data_col = 1 OR data_col = 2 " & _
" OR data_col = 3 OR data_col = 4 OR data_col = 5 " & _
" OR data_col = 6 OR data_col = 7 OR data_col = 8 " & _
" OR data_col = 9 OR data_col = 10 OR data_col = 11 " & _
" OR data_col = 12 OR data_col = 13 OR data_col = 14 " & _
" OR data_col = 15 OR data_col = 16 OR data_col = 17 " & _
" OR data_col = 18 OR data_col = 19 OR data_col = 20 " & _
" ) " & _
");"
.Execute Sql
Dim rs
' 5 = adSchemaCheckConstraints
Set rs = .OpenSchema(5)
MsgBox rs.GetString
End With
Set .ActiveConnection = Nothing
End With
End Sub
輸出顯示而對名爲data_col__be_positive
約束定義的確可以萃取,將data_col__values
定義不能(由於它超過255個字符)。
因此,真正的解決方案是始終保留您用於創建的代碼並隨後更改表格。對於我來說,使用SQL DDL腳本達到很好的意義(我不需要通過DDL無法創建的少數功能)。
如果您只需要複製表模式或表模式*和*其數據,則不提。 – 2009-09-03 10:56:38