如果用戶在網(wǎng)站連接access數(shù)據(jù)庫時(shí)出現(xiàn)錯(cuò)誤(未發(fā)現(xiàn)數(shù)據(jù)源或驅(qū)動(dòng)程序),請(qǐng)改用以下連接串連接數(shù)據(jù)庫。
conn.Open
"driver={microsoft access driver (*.mdb)};dbq=" & Server.MapPath("數(shù)據(jù)庫名.mdb")
或
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=數(shù)據(jù)庫密碼;Data Source=" & Server.MapPath("數(shù)據(jù)庫名.mdb") |
存取數(shù)據(jù)庫的原理
在ASP中,用來存取數(shù)據(jù)庫的對(duì)象統(tǒng)稱ADO對(duì)象(Active
Data Objects),主要含有三種對(duì)象:Connection、Recordset和Command,其中Connection負(fù)責(zé)打開或連接數(shù)據(jù)庫,Recordset負(fù)責(zé)存取數(shù)據(jù)表,Command負(fù)責(zé)對(duì)數(shù)據(jù)庫執(zhí)行行動(dòng)查詢(Action
Query)命令和執(zhí)行SQL Server的Stored Procedure。只依靠這三個(gè)對(duì)象還是無法存取數(shù)據(jù)庫的,還必須具有數(shù)據(jù)庫存取的驅(qū)動(dòng)程序:OLE
DB驅(qū)動(dòng)程序和ODBC驅(qū)動(dòng)程序。對(duì)于任何一種數(shù)據(jù)庫都必須有相對(duì)應(yīng)的OLE DB驅(qū)動(dòng)程序和ODBC驅(qū)動(dòng)程序,ADO對(duì)象才能對(duì)數(shù)據(jù)庫進(jìn)行存取。
連接數(shù)據(jù)庫和打開數(shù)據(jù)表
不同數(shù)據(jù)庫的連接方法有所不同(即建立Connection實(shí)例的方法不一樣),但建立Connection實(shí)例后,利用Recordset對(duì)象進(jìn)行存取數(shù)據(jù)的方法卻大同小異。下面對(duì)于不同的數(shù)據(jù)類型,編寫了相對(duì)應(yīng)的連接函數(shù),在程序中直接引用即可。
程序用VB Script腳本語言編寫。
1.
建立MdbRecordset對(duì)象。MDB數(shù)據(jù)庫是一個(gè)完整的數(shù)據(jù)庫,內(nèi)部可能含有若干個(gè)數(shù)據(jù)表,在此函數(shù)中,Connection的作用是連接數(shù)據(jù)庫,Recordset的作用是打開數(shù)據(jù)表。
Function CreateMdbRecordset(數(shù)據(jù)庫文件名, 數(shù)據(jù)表名或Select語句
)
Dim conn,Provider,DBPath
’建立Connection 對(duì)象
Set conn = Server.CreateObject
(“ADODB.Connection”)
Provider=“driver={microsoft access driver (*.mdb)};”
DBPath = “dbq=” & Server.MapPath(“數(shù)據(jù)庫文件名”)
’打開數(shù)據(jù)庫
conn.Open Provider & DBPath
Set CreateMdbRecordset = Server.CreateObject(“ADODB.Recordset”)
’打開數(shù)據(jù)表
CreateMdbRecordset.Open “數(shù)據(jù)表名”, conn, 2, 2
End Function
2.
建立帶密碼的MDB數(shù)據(jù)庫的Recordset對(duì)象。它的建立方式與建立不帶密碼的MDB數(shù)據(jù)庫的Recordset對(duì)象類似,只是多了一個(gè)密碼參數(shù),即在與數(shù)據(jù)庫連接時(shí),必須給出密碼信息。
Function CreateSecuredMdbRecordset( 數(shù)據(jù)庫文件名,
數(shù)據(jù)表名或Select語句,password )
Dim conn,Provider,DBPath
’建立Connection 對(duì)象
Set conn = Server.CreateObject
(“ADODB.Connection”)
Provider = “Provider=Microsof.Jet.OLEDB.4.0;”
DBPath = “Data Source=”& Server.MapPath(“數(shù)據(jù)庫文件名”)
’連接數(shù)據(jù)庫,注意要帶有密碼參數(shù)
conn.Open Provider & DBPath&“Jet OLEDB:Database
Password=”&assword
Set CreateSecuredMdbRecordset = Server.
CreateObject(“ADODB.Recordset”)
’打開數(shù)據(jù)表
CreateSecuredMdbRecordset.Open “數(shù)據(jù)表名”, conn,
2, 2
End Function
3.DBF文件不是一個(gè)標(biāo)準(zhǔn)的數(shù)據(jù)庫文件,只相當(dāng)于標(biāo)準(zhǔn)數(shù)據(jù)庫文件中的一個(gè)數(shù)據(jù)表,所以為了使用DBF文件,可以把所有的DBF文件放在一個(gè)目錄下,這樣把目錄名看成標(biāo)準(zhǔn)數(shù)據(jù)庫,每一個(gè)DBF文件相當(dāng)于標(biāo)準(zhǔn)數(shù)據(jù)庫中的數(shù)據(jù)表。下面函數(shù)中的Directory是DBF所在的目錄名。
Function CreateDbfRecordset( 目錄名, DBF文件名或Select語句
)
Dim conn,Driver,SourceType,DBPath
’建立Connection 對(duì)象
Set conn = Server.CreateObject
(“ADODB.Connection”)
Driver=“Driver={Microsoft Visual FoxProDriver};”
SourceType = “SourceType=DBF;”
DBPath=“SourceDB=” & Server.MapPath(“目錄名”)
’調(diào)用Open 方法打開數(shù)據(jù)庫
conn.Open Driver & SourceType & DBPath
Set CreateDbfRecordset = Server.CreateObject(“ADODB.Recordset”)
’打開DBF文件
CreateDbfRecordset.Open “DBF文件名或Select語句”, conn,
2, 2
End Function
4.
由FoxPro生成的DBC數(shù)據(jù)庫與MDB數(shù)據(jù)庫相似,都是一個(gè)數(shù)據(jù)庫包含幾個(gè)數(shù)據(jù)表的形式,所以對(duì)DBC數(shù)據(jù)庫的存取方法與MDB數(shù)據(jù)庫相似。
Function CreateDbcRecordset( DBC數(shù)據(jù)庫文件名, 數(shù)據(jù)表名或Select語句
)
Dim conn,Driver,SourceType,DBPath
’建立Connection 對(duì)象
Set conn = Server.CreateObject
(“ADODB.Connection”)
Driver=“Driver={Microsoft Visual FoxPro Driver};”
SourceType = “SourceType=DBC;”
DBPath = “SourceDB=” & Server.MapPath(“DBC數(shù)據(jù)庫文件名”)
’連接數(shù)據(jù)庫
conn.Open Driver & SourceType & DBPath
Set CreateDbcRecordset = Server.CreateObject(“ADODB.Recordset”)
’打開數(shù)據(jù)表
CreateDbcRecordset.Open“數(shù)據(jù)表名或Select語句”, conn,
2, 2
End Function
5.
將Excel生成的XLS文件(book)看成一個(gè)數(shù)據(jù)庫,其中的每一個(gè)工作表(sheet)看成一個(gè)數(shù)據(jù)庫表。
Function CreateExcelRecordset(XLS文件名,Sheet名)
Dim conn.Driver,DBPath
’建立Connection對(duì)象
Set conn = Server.CreateObject
(“ADODB.Connection”)
Driver=“Driver={Microsoft Excel Driver (*.xls)};”
DBPath = “DBQ=” & Server.MapPath(“XLS文件名”)
’調(diào)用Open 方法打開數(shù)據(jù)庫
conn.Open Driver & DBPath
Set CreateExcelRecordset = Server.CreateObject(“ADODB.Recordset”)
’打開Sheet
CreateExcelRecordset.Open “Select * From [”&sheet&“$]”,
conn, 2, 2
End Function
6.SQL Server屬于Server級(jí)的數(shù)據(jù)庫,使用時(shí)要求比較嚴(yán)格,必須要輸入用戶名及密碼才能使用。
Function CreateSQLServerRecordset(計(jì)算機(jī)名稱,用戶ID,
用戶密碼,數(shù)據(jù)庫名稱 數(shù)據(jù)表或查看表或Select指令 )
Dim Params, conn
Set CreatSQLServerConnection = Nothing
Set conn = Server.CreateObject
(“ADODB.Connection”)
Params = “Provider=SQLOLEDB.1”
Params = Params & “;Data Source=” & Computer
Params = Params & “;User ID=” & UserID
Params = Params & “;Password=” & Password
Params = Params & “.Initial Catalog=”&數(shù)據(jù)庫名稱
Conn open Paras
Set CreateSQLServerRecordset = Server.
CreateObject(“ADODB.Recordset")
CreateSQLServerRecordset.Open source, conn,
2, 2
End Function
|