'用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为数据库的全路径名
用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
'引用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为数据库的全路径名
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
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"
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