asp快速开发方法之数据操作实例代码第2/3页

 更新时间:2007年08月08日 11:05:52   作者:  

现在我们的showit.asp可以这样写:
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("文章标题是:"& rs("cn_title"))
    response.write("<br>文章作者是:"& rs("cn_author"))
    response.write("<br>文章加入时间是:"& rs("cn_time"))
    response.write("<br>文章内容是:"& rs("cn_content"))
    response.write("<hr>")
    rs.MoveNext
    Loop
else
    response.write ("暂时还没有文章")
end if
Closedatabase
%>
嗯,我们又少写了一些东西,这样是最简单的吗?当然不是!还可以更简单。
使用GetRows把查询出来的数据传给一个变量,使用ubound方法取得数据记录条数。
不明白?没关系,让我们继续往下看:

再建个文件: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
%>
嗯,复制它就可以了,现在我们的showit.asp可以简单地这样写:

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("文章标题是:"& Thedata(1,i))
        response.write("<br>文章作者是:"& Thedata(2,i))
        response.write("<br>文章加入时间是:"& Thedata(3,i))
        response.write("<br>文章内容是:"& Thedata(4,i))
        response.write("<hr>")
    next
else
    response.write("暂时还没有文章")
End If
%>
呵呵,这样,我们只要用两句语句就完成了数据的读取。同样的,通过在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)
%>
来取得记录条数,可以用于分页或者用户名是否重复的判断。

其它的对数据记录的操作我们新建一个类,使用UpdateTable来完成操作:
<%
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)
%>

相关文章

最新评论