Microsoft Office Access是由微軟發(fā)布的關系數據庫管理系統(tǒng)。它結合了 MicrosoftJet Database Engine 和 圖形用戶界面兩項特點,是 Microsoft Office 的系統(tǒng)程序之一。Microsoft Office Access是微軟把數據庫引擎的圖形用戶界面和軟件開發(fā)工具結合在一起的一個數據庫管理系統(tǒng)。它是微軟OFFICE的一個成員, 在包括專業(yè)版和更高版本的office版本里面被單獨出售。2018年9月25日,最新的微軟Office Access 2019在微軟Office 2019里發(fā)布。 盡管我們可以通過設計器來創(chuàng)建數據庫, 但是我們也可以在asp的代碼中創(chuàng)建數據庫,這里我們就一起來看一下如何在asp中創(chuàng)建數據庫. 在ASP中創(chuàng)建數據庫,我們需要用到ADOX(Microsoft ADO Extensions for DDL and Security), 這個ADO的擴展可以幫助我們創(chuàng)建和修改數據庫結構信息, 也包括數據庫對象的安全策略. 它隨著ADO 2.1 出現, 所以它能夠在大多數的Windows平臺上工作. 您可以到MS的官方網站去獲取最新的ADO版本,當然,里邊包括了ADOX. 創(chuàng)建數據庫 在我們開始代碼編寫之前,確定IIS所對應的帳號IUSER_[MachineName](MachineName:一般是你的計算機名) 擁有對您要創(chuàng)建數據庫的目錄有寫入權限。你也可以打開要保存數據庫文件的目錄的屬性對話框,找到安全選項,添加上述用戶的寫入權限。 為了順利創(chuàng)建數據庫,我們首先需要創(chuàng)建一個空的數據庫對象,然后我們才能創(chuàng)建一個新表和定義表的各列。這里有個重要的一點兒就是說,我們創(chuàng)建表的時候,必須在創(chuàng)建完數據庫后關閉數據連接。否則我們將沒有辦法創(chuàng)建數據庫和定義數據列。這就是為什么,我會在接下來創(chuàng)建兩個方法:CreateAccessDB(創(chuàng)建數據庫), CreateAccessTB(創(chuàng)建數據表),變量DBName用來定義要添加數據庫的名字,phyPath用來定義存放數據庫文件的路徑。下邊我們來看代碼:
這段代碼包含了一個adovbs.inc文件,這是個非常有用的文件,它定義了ADO和ADOX中用到的所有數值型變量,你可以在代碼中找到該文件,也可以去你自己電腦上:C:Program FilesCommon FilesSystemado下找到。如果需要在你的頁面中間引用,需要復制到網站自己的目錄下邊。 下邊是創(chuàng)建數據庫的代碼:
數據庫創(chuàng)建完了,接下來該表了,否則我們要一個沒有表的數據庫是毫無意義的。下邊是創(chuàng)建表的代碼:
1 Sub CreateAccessTB(DBToCreate) 2 Dim catDB ' As ADOX.Catalog 3 Set catDB = Server.CreateObject("ADOX.Catalog") 4 ' Open the catalog 5 catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 6 "Data Source=" & Server.Mapath(DBToCreate) 7 Dim tblNew ' As ADOX.Table 8 Set tblNew = Server.CreateObject("ADOX.Table") 9 tblNew.Name = TBName 10 ' First Create an Autonumber column, called ID. 11 ' This is just for demonstration purposes. 12 ' You could have done this below with all the other columns as well 13 Dim col ' As ADOX.Column 14 Set col = Server.CreateObject("ADOX.Column") 15 With col 16 ParentCatalog = catDB 17 .Type = adInteger 18 .Name = "ID" 19 .Properties("Autoincrement") = True 20 End With 21 ' Now add the rest of the columns 22 With tblNew 23 ' Create fields and append them to the 24 ' Columns collection of the new Table object. 25 With .Columns 26 .Append "NumberColumn", adInteger 27 .Append "FirstName", adVarWChar 28 .Append "LastName", adVarWChar 29 .Append "Phone", adVarWChar 30 .Append "Notes", adLongVarWChar 31 End With 32 33 Dim adColNullable ' Is not defined in adovbs.inc, 34 ' so you need to define it here. 35 ' The other option is adColFixed with a value of 1 36 adColNullable = 2 37 With .Columns("FirstName") 38 .Attributes = adColNullable 39 End With 40 End With 41 ' Add the new Table to the Tables collection of the database. 42 catDB.Tables.Append tblNew 43 Set col = Nothing 44 Set tblNew = Nothing 45 Set catDB = Nothing 46 End Sub
然后,可以在需要的地方調用:
1' First call the Create Database method 2 CreateAccessDB DBName 3 4 ' Then add a table and columns to this database 5 CreateAccessTB DBName Microsoft Access在很多地方得到廣泛使用,例如小型企業(yè),大公司的部門。 |
溫馨提示:喜歡本站的話,請收藏一下本站!