用ASP連接DBF、DBC、MDB、Excel、SQL Server型數(shù)據(jù)庫的方法:
一、ASP的對(duì)象存取數(shù)據(jù)庫方法
在ASP中,用來存取數(shù)據(jù)庫的對(duì)象統(tǒng)稱ADO(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)查詢命令
二、連接各數(shù)據(jù)庫的驅(qū)動(dòng)程序
連接各數(shù)據(jù)庫可以使用驅(qū)動(dòng)程序,也可以使用數(shù)據(jù)源,不過我建議大家使用驅(qū)動(dòng)程序,因?yàn)槭褂抿?qū)動(dòng)程序非常方便、簡(jiǎn)單,而使用數(shù)據(jù)源比較麻煩。
驅(qū)動(dòng)程序 適用數(shù)據(jù)庫類型
Microsoft.Jet.OLEDB.4.0 Mdb
Microsoft Access Driver Access
Microsoft dBase Driver Dbase
Microsoft Excel Driver Excel
Microsoft Visual FoxPro Driver Dbc
SQLOLEDB.1 SQL srver7.0
而我們?cè)谝话闱闆r下使用Access的數(shù)據(jù)庫比較多,在這里我建議大家連接Access數(shù)據(jù)庫使用下面的方法:
dim conn
set conn = server.createobject("adodb.connection")
conn.open = "provider=microsoft.jet.oledb.4.0;" & "data source = " &
server.mappath("../db/bbs.mdb")
其中../db/bbs.mdb是你的數(shù)據(jù)庫存放的相對(duì)路徑!如果你的數(shù)據(jù)庫和ASP文件在同一目錄下,你只要這樣寫就可以了:
dim conn
set conn = server.createobject("adodb.connection")
conn.open = "provider=microsoft.jet.oledb.4.0;" & "data source = " & server.mappath("bbs.mdb")
有許多初學(xué)者在遇到數(shù)據(jù)庫連接時(shí)總是會(huì)出問題,然而使用上面的驅(qū)動(dòng)程序只要你的數(shù)據(jù)庫路徑選對(duì)了就不會(huì)出問題了
ASP連接數(shù)據(jù)庫的5種方法
第一種 - 這種方法用在ACCESS中最多
strconn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" _
& Server.MapPath("aspfree.mdb")
set conn = server.createobject("adodb.connection")
conn.open strconn
第二種-這種方法用在SQL SERVER中多
strconn = "Driver={SQL Server};Description=sqldemo;SERVER=127.0.0.1;" _
&"UID=LoginID;Password=;DATABASE=Database_Name"
set conn = server.createobject("adodb.connection")
conn.open strconn
第三種
strconn="Driver={Microsoft Access Driver(*.mdb)};" _
&"DBQ=F:\Inetpub\wwwroot\somedir\db1.mdb;DefaultDir=" _
&"f:\Inetpub\wwwroot\somedir;uid=LoginID;" _
&"pwd=Password;DriverId=25;FIL=MSAccess;"
set conn = server.createobject("adodb.connection")
conn.open strconn
第四種運(yùn)用系統(tǒng)數(shù)據(jù)源
The following uses a Data Source Name: Example
set conn = server.createobject("adodb.connection")
conn.open "Example"
第五種運(yùn)用ODBC數(shù)據(jù)源,前提是你必須在控制面板的ODBC中設(shè)置數(shù)據(jù)源
set rs = server.createobject("adodb.recordset")
rs.open "tblname", "DSNName", 3, 3 第一種 - 這種方法用在ACCESS中最多
strconn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" _
& Server.MapPath("aspfree.mdb")
set conn = server.createobject("adodb.connection")
conn.open strconn
ASP如何使用MYSQL數(shù)據(jù)庫
mysql數(shù)據(jù)庫以它短小、方便、速度快、免費(fèi)等優(yōu)點(diǎn)成為很多網(wǎng)站目前首選
數(shù)據(jù)庫,但一般都是用php+mysql相結(jié)合來開發(fā)各種動(dòng)態(tài)頁面,其實(shí)asp也可以
使用mysql數(shù)據(jù)庫開發(fā)動(dòng)態(tài)頁面,小弟我也是剛剛學(xué)會(huì),不敢獨(dú)享,所以特寫了
這篇文章供大伙參考。
我的環(huán)境是windows2000+iis5.0+mysql-3.23.32-win+php4
mysql-3.23.32-win(這個(gè)是最新版的)
myodbc-2.50.36-dll(這個(gè)是最重要的,mysql odbc的驅(qū)動(dòng)程序,可以在www.mysql.com下載)
第一步:安裝mysql odbd的驅(qū)動(dòng)程序,將下載的myodbd-2.50.46-dll文件復(fù)制到windows\system目錄下(windows2000是winnt/system32)
然后建立一新文件,擴(kuò)展名為reg(就是注冊(cè)表文件),將以下內(nèi)容復(fù)制到該文件中。
regedit4
[hkey_local_machine\software\odbc\odbcinst.ini\myodbc driver]
"usagecount"=dword:00000002
"driver"="c:\\windows\\system\\myodbc.dll"
"setup"="c:\\windows\\system\\myodbc.dll"
"sqllevel"="1"
"fileusage"="0"
"driverodbcver"="02.50"
"connectfunctions"="yyy"
"apilevel"="1"
"cptimeout"="120"
[hkey_local_machine\software\odbc\odbcinst.ini\odbc drivers]
"myodbc driver"="installed"
保存后雙擊該文件,將上面代碼注冊(cè)到windows注冊(cè)表中。
如果安裝在windows2000中,則driver和setup主鍵的值要做相應(yīng)改變,這里我想就不用多說了。
如果成功,在控制面板/odbd數(shù)據(jù)源的驅(qū)動(dòng)程序里將看到myodbd driver這一項(xiàng)!
第二步:建立asp文件鏈接數(shù)據(jù)庫。
這里有兩種方法,一種是在odbc數(shù)據(jù)源中建立一個(gè)系統(tǒng)dsn。后來我發(fā)現(xiàn)不建立也可以在asp中使用mysql,方法在下文將講道。
打開控制面板/odbd數(shù)據(jù)源,選擇系統(tǒng)dsn,然后添加一個(gè)新的dsn,驅(qū)動(dòng)程序選擇myodbd driver,會(huì)出現(xiàn)一個(gè)對(duì)話框供輸入mysql
相關(guān)信息。
windows dsn name: 所要建立dsn的名稱
mysql host (name or ip):mysql服務(wù)器的名稱或者是ip地址,通常填localhost
mysql database name:需要使用數(shù)據(jù)庫的名稱,數(shù)據(jù)庫在mysql管理程序中建立。這里我們使用一個(gè)例子。數(shù)據(jù)庫名:hc188
里面有數(shù)據(jù)表:user 數(shù)據(jù)表有兩個(gè)字段分別是:username和password,隨便插入幾個(gè)數(shù)據(jù)。
user:鏈接數(shù)據(jù)庫的用戶名,我填的是root超級(jí)用戶
password:鏈接數(shù)據(jù)庫用戶密碼,如果沒有,可以不填
port(if not 3306):mysql在服務(wù)器的端口,如果不填默認(rèn)為3306
sql command on connect:使用sql命令鏈接數(shù)據(jù)庫,這項(xiàng)可以不填
填寫完畢后選擇ok保存。
下面鏈接數(shù)據(jù)庫的asp代碼!
<%
strconnection = "dsn=hc188;driver={myodbd driver};server=localhost;uid=root;pwd=;database=hc188"
set adodataconn = server.createobject("adodb.connection")
adodataconn.open strconnection
strquery = "select * from user"
set rs = adodataconn.execute(strquery)
if not rs.bof then
%>
<table>
<tr>
<td<b>username</b></td>
<td><b>password</b></td>
</tr>
<%
do while not rs.eof
%>
<tr>
<td><%=rs("username")%></td>
<td><%=rs("password")%></td>
</tr>
<%
rs.movenext
loop
%>
</table>
<%
else
response.write("sorry, no data found.")
end if
rs.close
adodataconn.close
set adodataconn = nothing
set rsemaildata = nothing
%>
第二種方法:我在使用中想過如果不建立系統(tǒng)dsn,是否也可以使用mysql數(shù)據(jù)庫呢?結(jié)果是可以的。
方法很簡(jiǎn)單,把上面asp代碼第二行代碼改為:
strconnection="defaultdir=;driver={myodbc driver};database=hc188"
我奇怪的發(fā)現(xiàn),這種方法連用戶名和密碼都不需要就可以使用。是不是mysql的一個(gè)bug呢?
以上代碼全部經(jīng)測(cè)試通過!
ASP 編程中 20 個(gè)非常有用的例子
1.如何用Asp判斷你的網(wǎng)站的虛擬物理路徑
答:使用Mappath方法
< p align="center" >< font size="4" face="Arial" >< b >
The Physical path to this virtual website is:
< /b >< /font >
< font color="#FF0000" size="6" face="Arial" >
< %= Server.MapPath("\")% >
< /font >< /p >
2.我如何知道使用者所用的瀏覽器?
答:使用the Request object方法
strBrowser=Request.ServerVariables("HTTP_USER_AGENT")
If Instr(strBrowser,"MSIE") < > 0 Then
Response.redirect("ForMSIEOnly.htm")
Else
Response.redirect("ForAll.htm")
End If
3.如何計(jì)算每天的平均反復(fù)訪問人數(shù)
答:解決方法
< % startdate=DateDiff("d",Now,"01/01/1990")
if strdate< 0 then startdate=startdate*-1
avgvpd=Int((usercnt)/startdate) % >
顯示結(jié)果
< % response.write(avgvpd) % >
that is it.this page have been viewed since November 10,1998
4.如何顯示隨機(jī)圖象
< % dim p,ppic,dpic
ppic=12
randomize
p=Int((ppic*rnd)+1)
dpic="graphix/randompics/"&p&".gif"
% >
顯示
< img src="< %=dpic% >" >
5.如何回到先前的頁面
答:< a href="< %=request.serverVariables("Http_REFERER")% >" >preivous page< /a >
或用圖片如:< img src="arrowback.gif" alt="< %=request.serverVariables("HTTP_REFERER")% >" >
6.如何確定對(duì)方的IP地址
答:< %=Request.serverVariables("REMOTE_ADDR)% >
7.如何鏈結(jié)到一副圖片上
答:< % @Languages=vbscript % >
< % response.expires=0
strimagename="graphix/errors/erroriamge.gif"
response.redirect(strimagename)
% >
8.強(qiáng)迫輸入密碼對(duì)話框
答:把這句話放載頁面的開頭
< % response.status="401 not Authorized"
response.end
% >
9.如何傳遞變量從一頁到另一頁
答:用 HIDDEN 類型來傳遞變量
< % form method="post" action="mynextpage.asp" >
< % for each item in request.form % >
< input namee="< %=item% >" type="HIDDEN"
value="< %=server.HTMLEncode(Request.form(item)) % >" >
< % next % >
< /form >
10.為何我在 asp 程序內(nèi)使用 msgbox,程序出錯(cuò)說沒有權(quán)限
答:由于 asp 是服務(wù)器運(yùn)行的,如果可以在服務(wù)器顯示一個(gè)對(duì)話框,那么你只好等有人按了確定之后,你的程序才能繼續(xù)執(zhí)行,而一般服務(wù)器不會(huì)有人守著,所以微軟不得不禁止這個(gè)函數(shù),并胡亂告訴你 (:) 呵呵) 沒有權(quán)限。但是ASP和客戶端腳本結(jié)合倒可以顯示一個(gè)對(duì)話框,as follows:
< % yourVar="測(cè)試對(duì)話框"% >
< % script language='javascript' >
alert("< %=yourvar% >")
< /script >
11.有沒有辦法保護(hù)自己的源代碼,不給人看到
答:可以去下載一個(gè)微軟的Windows Script Encoder,它可以對(duì)asp的腳本和客戶端javascript/vbscript腳本進(jìn)行加密。。。不過客戶端加密后,只有ie5才能執(zhí)行,服務(wù)器端腳本加密后,只有服務(wù)器上安裝有script engine 5(裝一個(gè)ie5就有了)才能執(zhí)行。
12.怎樣才能將 query string 從一個(gè) asp 文件傳送到另一個(gè)?
答:前者文件加入下句:Response.Redirect("second.asp?" & Request.ServerVariables("QUERY_STRING"))
13.global.asa文件總是不起作用?
答:只有web目錄設(shè)置為web application, global.asa才有效,并且一個(gè)web application的根目錄下 global.asa才有效。IIS4可以使用Internet Service Manager設(shè)置application setting 怎樣才能使得htm文件如同asp文件一樣可以執(zhí)行腳本代碼?
14.怎樣才能使得htm文件如同asp文件一樣可以執(zhí)行腳本代碼?
答:Internet Sevices Manager - > 選擇default web site - >右鼠鍵- >菜單屬性-〉主目錄- > 應(yīng)用程序設(shè)置(Application Setting)- > 點(diǎn)擊按鈕 "配置"- > app mapping - >點(diǎn)擊按鈕"Add" - > executable browse選擇 \WINNT\SYSTEM32\INETSRV\ASP.DLL EXTENSION 輸入 htm method exclusions 輸入PUT.DELETE 全部確定即可。但是值得注意的是這樣對(duì)htm也要由asp.dll處理,效率將降低。
15.如何注冊(cè)組件
答:有兩種方法。
第一種方法:手工注冊(cè) DLL 這種方法從安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢】