Microsoft Office Access是由微軟發(fā)布的關(guān)系數(shù)據(jù)庫管理系統(tǒng)。它結(jié)合了 MicrosoftJet Database Engine 和 圖形用戶界面兩項(xiàng)特點(diǎn),是 Microsoft Office 的系統(tǒng)程序之一。Microsoft Office Access是微軟把數(shù)據(jù)庫引擎的圖形用戶界面和軟件開發(fā)工具結(jié)合在一起的一個(gè)數(shù)據(jù)庫管理系統(tǒng)。它是微軟OFFICE的一個(gè)成員, 在包括專業(yè)版和更高版本的office版本里面被單獨(dú)出售。2018年9月25日,最新的微軟Office Access 2019在微軟Office 2019里發(fā)布。 盡管我們可以通過設(shè)計(jì)器來創(chuàng)建數(shù)據(jù)庫, 但是我們也可以在asp的代碼中創(chuàng)建數(shù)據(jù)庫,這里我們就一起來看一下如何在asp中創(chuàng)建數(shù)據(jù)庫. 在ASP中創(chuàng)建數(shù)據(jù)庫,我們需要用到ADOX(Microsoft ADO Extensions for DDL and Security), 這個(gè)ADO的擴(kuò)展可以幫助我們創(chuàng)建和修改數(shù)據(jù)庫結(jié)構(gòu)信息, 也包括數(shù)據(jù)庫對象的安全策略. 它隨著ADO 2.1 出現(xiàn), 所以它能夠在大多數(shù)的Windows平臺(tái)上工作. 您可以到MS的官方網(wǎng)站去獲取最新的ADO版本,當(dāng)然,里邊包括了ADOX. 創(chuàng)建數(shù)據(jù)庫 在我們開始代碼編寫之前,確定IIS所對應(yīng)的帳號IUSER_[MachineName](MachineName:一般是你的計(jì)算機(jī)名) 擁有對您要?jiǎng)?chuàng)建數(shù)據(jù)庫的目錄有寫入權(quán)限。你也可以打開要保存數(shù)據(jù)庫文件的目錄的屬性對話框,找到安全選項(xiàng),添加上述用戶的寫入權(quán)限。 為了順利創(chuàng)建數(shù)據(jù)庫,我們首先需要?jiǎng)?chuàng)建一個(gè)空的數(shù)據(jù)庫對象,然后我們才能創(chuàng)建一個(gè)新表和定義表的各列。這里有個(gè)重要的一點(diǎn)兒就是說,我們創(chuàng)建表的時(shí)候,必須在創(chuàng)建完數(shù)據(jù)庫后關(guān)閉數(shù)據(jù)連接。否則我們將沒有辦法創(chuàng)建數(shù)據(jù)庫和定義數(shù)據(jù)列。這就是為什么,我會(huì)在接下來創(chuàng)建兩個(gè)方法:CreateAccessDB(創(chuàng)建數(shù)據(jù)庫), CreateAccessTB(創(chuàng)建數(shù)據(jù)表),變量DBName用來定義要添加數(shù)據(jù)庫的名字,phyPath用來定義存放數(shù)據(jù)庫文件的路徑。下邊我們來看代碼:
這段代碼包含了一個(gè)adovbs.inc文件,這是個(gè)非常有用的文件,它定義了ADO和ADOX中用到的所有數(shù)值型變量,你可以在代碼中找到該文件,也可以去你自己電腦上:C:Program FilesCommon FilesSystemado下找到。如果需要在你的頁面中間引用,需要復(fù)制到網(wǎng)站自己的目錄下邊。 下邊是創(chuàng)建數(shù)據(jù)庫的代碼:
數(shù)據(jù)庫創(chuàng)建完了,接下來該表了,否則我們要一個(gè)沒有表的數(shù)據(jù)庫是毫無意義的。下邊是創(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
然后,可以在需要的地方調(diào)用:
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è),大公司的部門。 |
溫馨提示:喜歡本站的話,請收藏一下本站!