使用VB 6和Access 2003
我想将一个表从一个数据库复制到另一个数据库。
数据库1
Table1
Table2
的Database2
Table3
上面,我想将Table3复制到Database-1
Expected Output
Table1
Table2
Table3
如何编写代码?
需要VB6代码帮助。
答案 0 :(得分:4)
使用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。
答案 1 :(得分:0)
请注意,即使同时使用ADO(CHECK
约束,WITH COMPRESSION
等所需的ADO)和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无法创建的少数功能)。