请教一下:急,如何用VB6创建一个.mdb文件(在线等待),如能同时加密更好。谢谢,希望具体说明。

解决方案 »

  1.   

    '用ADOX建立数据库与表
    '引用Microsoft ADO Ext 2.5 for DDL and Security
    Dim cat As ADOX.Catalog
    Dim tbl As ADOX.Table
    Dim con As ADODB.Connection    On Error GoTo 0    ' Create the new database.
        Set cat = New ADOX.Catalog
        cat.Create _
            "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & DatabaseName & ";"    ' Create a new table.
        Set tbl = New ADOX.Table
        tbl.Name = "TestTable"
        tbl.Columns.Append "FirstName", adVarWChar, 40
        tbl.Columns.Append "LastName", adVarWChar, 40
        tbl.Columns.Append "Birthdate", adDate
        tbl.Columns.Append "Weight", adInteger
        cat.Tables.Append tbl 
       
        '设置列可以为NULL    
        tb1.columns("Weight").Attributes=AdColNullable
       '或者tb1.Columns("Weight").Properties("Jet OLEDB:Allow Zero Length") = True

        ' Connect to the database.
        Set con = cat.ActiveConnection    ' Insert records.
        con.Execute "INSERT INTO TestTable VALUES ('Andy', 'Able', '1 Jan 1980', '150')"
        con.Execute "INSERT INTO TestTable VALUES ('Betty', 'Baker', #2/22/1990#, 70)"    ' Close the database connection.
        con.Close
        Set con = Nothing
        Set tbl = Nothing
        Set cat = Nothing
    =========================利用DAO可以建立一个数据库,范例如下:
    Dim db As Database' if desired, you can specify a version or encrypt
    ' the database as the optional third parameter to
    ' the CreateDatabase method
    Set db = DBEngine(0).CreateDatabase(strDBName, dbLangGeneral)
    其中strDBName为数据库的全路径名
      

  2.   

    用adox,参考:Microsoft OLE DB Provider for SQL Server
    Catalog object The Create method is not supported. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/pg_adox_fundamentals_2.asp你也可以在vb论坛内搜索adox
      

  3.   

    只需改一下:
        cat.Create _
            "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & DatabaseName & ";"
    改成:
        cat.Create _
            "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=c:\testnew.mdb;Jet OLEDB:Database Password=1234"
      

  4.   

    tbl.Columns.Append "Weight", adInteger
        cat.Tables.Append tbl 
       
        '设置列可以为NULL    
        tb1.columns("Weight").Attributes=AdColNullable
       '或者tb1.Columns("Weight").Properties("Jet OLEDB:Allow Zero Length") = True应该是:
     tbl.Columns.Append "Weight", adInteger 
        '设置列可以为NULL    
        tbl.columns("Weight").Attributes=AdColNullable
       '或者tbl.Columns("Weight").Properties("Jet OLEDB:Allow Zero Length") = True    cat.Tables.Append tbl