|  | 网站首页 | .Net研究 | 
您现在的位置: 程序员之路 >> .Net研究 >> .net应用技术 >> 具体编程点 >> 文章正文
赞助商链接
频 道 导 航
.Net研究频道栏目导航
相 关 文 章
收藏:如何根据当前MDB中的表生成对应的JET SQL DDL “CREATE TABL           
收藏:如何根据当前MDB中的表生成对应的JET SQL DDL “CREATE TABL
作者:flyingfi… 文章来源:不详 更新时间:2007-5-10 11:06:28

作者:cg1  摘自:access911.net 编辑

专题地址:

http://access911.net/?kbid;72FAB11E16DCEBF3

简述:

如何根据当前MDB中的表生成对应的JET SQL DDL “CREATE TABLE”语句?
如何根据这个脚本在当前数据库中新建表?
如何根据当前MDB中的表生成对应的JET SQL DDL “CREATE TABLE”语句/脚本?《查询》

阅读前需掌握:

熟练掌握 VBA 编程;熟练掌握 JET SQL 语句

问题:

如何根据当前MDB中的表生成对应的JET SQL DDL “CREATE TABLE”语句?
如何用这个脚本在一个新的数据库中新建表?
SQL SERVER可以将表结构导出为 *.SQL 的脚本,这个*.sql脚本里面是一些 "Create table" 语句,ACCESS 能做到么?

回答:

Access并未内置将表结构导出为脚本下次能直接建表的功能。

利用 ADOX / ADO / DAO 三个数据访问模型来获取对应的信息并组织 JET SQL DDL 语句,生成对应的 *.jetsql 文本文件脚本。再根据上述脚本在一个新的 MDB 数据库中新建上述表。

注意:由于 JET SQL DDL 语句并不支持所有的 ADOX / ADO / DAO 属性,所以有一部分表的属性,比如“格式”属性无法通过 JET SQL DDL 语句建立。最完整的解决方案是生成 *.VBA 脚本,而不是 JET SQL 脚本。

Function CreateSQLString(ByVal FilePath As String) As Boolean
'本函数根据当前MDB中的表创建一个 *.jetsql 脚本
'这个函数不是最完美的解决方案,因为 JET SQL DDL 语句不支持一些 ACCESS 特有的属性(DAO支持)
'This function create a "*.jetsql" script based on current mdb tables.
'This function is not the BEST, because the JET SQL DDL never support some DAO property.

    Dim MyTableName As String
    Dim MyFieldName As String
    Dim MyDB As New ADOX.Catalog
    Dim MyTable As ADOX.Table
    Dim MyField As ADOX.Column
    Dim pro
    Dim iC As Long
    Dim strField() As String
    Dim strKey As String
    Dim strSQL As String
    Dim strSQLScript As String
    Dim objFile, stmFile
    Dim strText As String

On Error GoTo CreateSQLScript_Err
    MyDB.ActiveConnection = CurrentProject.Connection
    For Each MyTable In MyDB.Tables
        If MyTable.Type = "TABLE" Then
'指定表的类型,例如“TABLE”、“SYSTEM TABLE”或“GLOBAL TEMPORARY”或者“ACCESS TABLE”。
        'ADOX 无法判断该表是否已经被删除,还有两种方式判断,
        '方法一:(用 DAO)
        'If CurrentDb.TableDefs(strTableName).Attributes = 0 Then
        '方法二:(在判断 ADOX.Table.Type 的基础上再判定表名)
        'If Left(MyTable.Name, 7) <> "~TMPCLP" Then
            strSQL = "create table [" & MyTable.Name & "]("
            For Each MyField In MyTable.Columns
                ReDim Preserve strField(iC)
                strField(iC) = SQLField(MyField)
                iC = iC + 1
            Next
            strSQL = strSQL & Join(strField, ",")
'获取当前表的字段信息后立即重新初始化 strField 数组
            iC = 0
            ReDim strField(iC)
'加入键信息
            strKey = SQLKey(MyTable)
            If Len(strKey) <> 0 Then
                strSQL = strSQL & "," & strKey
            End If
            strSQL = strSQL & ");" & vbCrLf
            strSQLScript = strSQLScript & strSQL
'Debug.Print SQLIndex(MyTable)      'Never support the INDEX,to be continued...
            '暂未支持 index 脚本,未完待续...
        End If
    Next
    Set MyDB = Nothing

'create the Jet SQL Script File
    Set objFile = CreateObject("Scripting.FileSystemObject")
    Set stmFile = objFile.CreateTextFile(FilePath, True)
    stmFile.Write strSQLScript
    stmFile.Close
    Set stmFile = Nothing
    Set objFile = Nothing

    CreateSQLScript = True

CreateSQLScript_Exit:
    Exit Function

CreateSQLScript_Err:
    MsgBox Err.Description, vbExclamation
    CreateSQLScript = False
    Resume CreateSQLScript_Exit

End Function

Function RunFromText(ByVal FilePath As String)
'本函数将 CreateSQLScript 生成的 *.jetsql 脚本来生成 mdb 数据库中的表
'This Function run the "*.jetsql" which is created by CreateSQLScript to create the tables in current mdb database.
On Error Resume Next
    Dim objFile, stmFile
    Dim strText As String
    Set objFile = CreateObject("Scripting.FileSystemObject")
    Set stmFile = objFile.OpenTextFile(FilePath, 1, False)
    strText = stmFile.ReadAll
    stmFile.Close
    Set stmFile = Nothing
    Set objFile = Nothing
    Dim strSQL() As String
    Dim i As Long
    strSQL = Split(strText, ";" & vbCrLf)
    For i = LBound(strSQL) To UBound(strSQL)
        CurrentProject.Connection.Execute Trim(strSQL(i))
        If Err <> 0 Then
            Debug.Print "Error SQL is:" & strSQL(i)
            Err.Clear
        End If
    Next
End Function

Function SQLKey(ByVal objTable As ADOX.Table)
'调用 ADOX 生成有关“键”的 JET SQL DDL 子句
'Reference ADOX and create the JET SQL DDL clause about the "Key"

    Dim MyKey As ADOX.Key
    Dim MyKeyColumn As ADOX.Column
    Dim strKey As String
    Dim strColumns() As String
    Dim strKeys() As String
    Dim i As Long
    Dim iC As Long
    For Each MyKey In objTable.Keys
        Select Case MyKey.Type
        Case adKeyPrimary
            strKey = "Primary KEY "
        Case adKeyForeign
            strKey = "FOREIGN KEY "
        Case adKeyUnique
            strKey = "UNIQUE "
        End Select
        For Each MyKeyColumn In MyKey.Columns
            Re

[1] [2] 下一页

文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • .Net研究栏目导航
    网站频道导航