如果用戶在網(wǎng)站連接access數(shù)據(jù)庫時出現(xiàn)錯誤(未發(fā)現(xiàn)數(shù)據(jù)源或驅動程序),請改用以下連接串連接數(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ù)庫的對象統(tǒng)稱ADO對象(Active
Data Objects),主要含有三種對象:Connection、Recordset和Command,其中Connection負責打開或連接數(shù)據(jù)庫,Recordset負責存取數(shù)據(jù)表,Command負責對數(shù)據(jù)庫執(zhí)行行動查詢(Action
Query)命令和執(zhí)行SQL Server的Stored Procedure。只依靠這三個對象還是無法存取數(shù)據(jù)庫的,還必須具有數(shù)據(jù)庫存取的驅動程序:OLE
DB驅動程序和ODBC驅動程序。對于任何一種數(shù)據(jù)庫都必須有相對應的OLE DB驅動程序和ODBC驅動程序,ADO對象才能對數(shù)據(jù)庫進行存取。
連接數(shù)據(jù)庫和打開數(shù)據(jù)表
不同數(shù)據(jù)庫的連接方法有所不同(即建立Connection實例的方法不一樣),但建立Connection實例后,利用Recordset對象進行存取數(shù)據(jù)的方法卻大同小異。下面對于不同的數(shù)據(jù)類型,編寫了相對應的連接函數(shù),在程序中直接引用即可。
程序用VB Script腳本語言編寫。
1.
建立MdbRecordset對象。MDB數(shù)據(jù)庫是一個完整的數(shù)據(jù)庫,內部可能含有若干個數(shù)據(jù)表,在此函數(shù)中,Connection的作用是連接數(shù)據(jù)庫,Recordset的作用是打開數(shù)據(jù)表。
Function CreateMdbRecordset(數(shù)據(jù)庫文件名, 數(shù)據(jù)表名或Select語句
)
Dim conn,Provider,DBPath
’建立Connection 對象
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對象。它的建立方式與建立不帶密碼的MDB數(shù)據(jù)庫的Recordset對象類似,只是多了一個密碼參數(shù),即在與數(shù)據(jù)庫連接時,必須給出密碼信息。
Function CreateSecuredMdbRecordset( 數(shù)據(jù)庫文件名,
數(shù)據(jù)表名或Select語句,password )
Dim conn,Provider,DBPath
’建立Connection 對象
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文件不是一個標準的數(shù)據(jù)庫文件,只相當于標準數(shù)據(jù)庫文件中的一個數(shù)據(jù)表,所以為了使用DBF文件,可以把所有的DBF文件放在一個目錄下,這樣把目錄名看成標準數(shù)據(jù)庫,每一個DBF文件相當于標準數(shù)據(jù)庫中的數(shù)據(jù)表。下面函數(shù)中的Directory是DBF所在的目錄名。
Function CreateDbfRecordset( 目錄名, DBF文件名或Select語句
)
Dim conn,Driver,SourceType,DBPath
’建立Connection 對象
Set conn = Server.CreateObject
(“ADODB.Connection”)
Driver=“Driver={Microsoft Visual FoxProDriver};”
SourceType = “SourceType=DBF;”
DBPath=“SourceDB=” & Server.MapPath(“目錄名”)
’調用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ù)庫相似,都是一個數(shù)據(jù)庫包含幾個數(shù)據(jù)表的形式,所以對DBC數(shù)據(jù)庫的存取方法與MDB數(shù)據(jù)庫相似。
Function CreateDbcRecordset( DBC數(shù)據(jù)庫文件名, 數(shù)據(jù)表名或Select語句
)
Dim conn,Driver,SourceType,DBPath
’建立Connection 對象
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)看成一個數(shù)據(jù)庫,其中的每一個工作表(sheet)看成一個數(shù)據(jù)庫表。
Function CreateExcelRecordset(XLS文件名,Sheet名)
Dim conn.Driver,DBPath
’建立Connection對象
Set conn = Server.CreateObject
(“ADODB.Connection”)
Driver=“Driver={Microsoft Excel Driver (*.xls)};”
DBPath = “DBQ=” & Server.MapPath(“XLS文件名”)
’調用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級的數(shù)據(jù)庫,使用時要求比較嚴格,必須要輸入用戶名及密碼才能使用。
Function CreateSQLServerRecordset(計算機名稱,用戶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
|