asp快速開(kāi)發(fā)方法之?dāng)?shù)據(jù)操作實(shí)例代碼第2/3頁(yè)
更新時(shí)間:2007年08月08日 11:05:52 作者:
現(xiàn)在我們的showit.asp可以這樣寫(xiě):
showit.asp
<!--#include file="conn.asp" -->
<%
sql = "Select * from cnarticle"
opendatabase
rs.Open sql,conn,1,1
If not Rs.eof then
Do Until rs.EOF
response.write("文章標(biāo)題是:"& rs("cn_title"))
response.write("<br>文章作者是:"& rs("cn_author"))
response.write("<br>文章加入時(shí)間是:"& rs("cn_time"))
response.write("<br>文章內(nèi)容是:"& rs("cn_content"))
response.write("<hr>")
rs.MoveNext
Loop
else
response.write ("暫時(shí)還沒(méi)有文章")
end if
Closedatabase
%>
嗯,我們又少寫(xiě)了一些東西,這樣是最簡(jiǎn)單的嗎?當(dāng)然不是!還可以更簡(jiǎn)單。
使用GetRows把查詢出來(lái)的數(shù)據(jù)傳給一個(gè)變量,使用ubound方法取得數(shù)據(jù)記錄條數(shù)。
不明白?沒(méi)關(guān)系,讓我們繼續(xù)往下看:
再建個(gè)文件:sql.asp
sql.asp
<%
Class selectDataTable
public Function SelectData(sql)
If sql<>"" then
opendatabase
Rs.open sql,conn,1,1
If not Rs.eof then
Thedata=Rs.GetRows(-1)
Closedatabase
Else
Closedatabase
End If
End If
SelectData=Thedata
End Function
End Class
%>
嗯,復(fù)制它就可以了,現(xiàn)在我們的showit.asp可以簡(jiǎn)單地這樣寫(xiě):
showit.asp
<!--#include file="conn.asp" -->
<!--#include file="sql.asp" -->
<%
sql = "Select * from cnarticle"
set loadData=new selectDataTable
Thedata=loadData.SelectData(sql)
If isarray(Thedata) then
Num=ubound(Thedata,2)
for i=0 to Num
response.write("文章標(biāo)題是:"& Thedata(1,i))
response.write("<br>文章作者是:"& Thedata(2,i))
response.write("<br>文章加入時(shí)間是:"& Thedata(3,i))
response.write("<br>文章內(nèi)容是:"& Thedata(4,i))
response.write("<hr>")
next
else
response.write("暫時(shí)還沒(méi)有文章")
End If
%>
呵呵,這樣,我們只要用兩句語(yǔ)句就完成了數(shù)據(jù)的讀取。同樣的,通過(guò)在sql.asp中加入
<%
public Function SelectDataNum(sql)
If sql<>"" then
Opendatabase
Rs.open sql,conn,1,1
If not Rs.eof then
Thedata=Rs.GetRows(-1)
Closedatabase
Num=ubound(Thedata,2)
Else
Closedatabase
End If
End If
SelectDataNum=Num
End Function
%>
我們就可以使用
<%
sql = "Select * from cnarticle"
set loadData=new selectDataTable
Num=loadData.SelectDataNum(sql)
%>
來(lái)取得記錄條數(shù),可以用于分頁(yè)或者用戶名是否重復(fù)的判斷。
其它的對(duì)數(shù)據(jù)記錄的操作我們新建一個(gè)類,使用UpdateTable來(lái)完成操作:
<%
Class UpdataTable
public Function UpdataSql(sql)
If sql<>"" then
Opendatabase
conn.execute(sql)
Closedatabase
End If
End Function
End Class
%>
<%
sql = "delete from cnarticle"
set UpdateDate=new UpdataTable
UpdateDate.UpdataSql(sql)
%>
相關(guān)文章
asp 存貯過(guò)程 (SQL版asp調(diào)用存儲(chǔ)過(guò)程)
asp 存貯過(guò)程 (SQL版asp調(diào)用存儲(chǔ)過(guò)程)...2007-11-11
Eval 函數(shù) | Execute 語(yǔ)句 | ExecuteGlobal 語(yǔ)句使用說(shuō)明
在運(yùn)行時(shí)添加過(guò)程和類是非常有用的,但是也可能導(dǎo)致在運(yùn)行時(shí)覆蓋已有的全局 變量 和函數(shù)。因?yàn)檫@可能導(dǎo)致非常嚴(yán)重的程序問(wèn)題,因此,當(dāng)使用 ExecuteGlobal 語(yǔ)句時(shí)一定得非常謹(jǐn)慎。2007-02-02
ASP編程入門(mén)進(jìn)階(二十):ADO組件之查詢數(shù)據(jù)記錄
ASP編程入門(mén)進(jìn)階(二十):ADO組件之查詢數(shù)據(jù)記錄...2007-01-01
生成靜態(tài)頁(yè)大全[ASP/PHP/ASPX]
生成靜態(tài)頁(yè)大全[ASP/PHP/ASPX]...2006-07-07
IIS 錯(cuò)誤 Server Application Error 詳細(xì)解決方法
IIS 錯(cuò)誤 Server Application Error 詳細(xì)解決方法...2007-02-02

