当前位置: 首页 > 图文教程 > 网络编程 > ASP > ASP快速开发方法之数据操作

ASP
ASP正则表达式技巧
ASP Access实现网站计数器(访问量)
ASP新闻分页,将一篇过长的文章分页,生成静态页面
一些关于asp 购物车的想法
一个sql查询器,自动画表格填字段
ASP实现文件直接下载的代码
把网页中的(电话,qq等数字)生成图片的ASP程序
asp长文章用分页符来分页显示
Asp函数介紹(37个常用函数)
ASP中的面向对象类
分页实现方法的性能比较
asp ajax跨域提交数据
asp修改文件和文件夹的名字的代码
ASP 多关键词查询实例代码
asp被杀毒软件误删的解决方法
asp 多关键词搜索的简单实现方法
asp 根据IP地址自动判断转向分站的代码
asp dictionary对象的用法
ASP 千万级数据分页的存储过程
ASP隐藏真实文件的下载功能实现代码

ASP快速开发方法之数据操作


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 146 ::
收藏到网摘: n/a

这是我自己的心得,给大家作个参考。
我的目的是让开发变得简单,尽可能少地考虑实现语句,更多地把精力用于思考业务逻辑。希望我的文章对大家有所启发和帮助。

好吧,让我们进入正题:

先看以下例子:

以下为引用的内容:
<%
db_path = "database/cnbruce.mdb"
Set conn= Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath(db_path)
conn.Open connstr
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from cnarticle"
rs.Open sql,conn,1,1
if rs.EOF and rs.BOF then
response.write ("暂时还没有文章")
else
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
end if
rs.close
Set rs = Nothing
conn.close
set conn=Nothing
%>

嗯,这是一个典型的读取数据并显示的例子,参见:http://www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=448
嗯,确实简单。从上至下,很容易明白。但是当你对多个表进行读插删改的时候,当你的代码里有很多HTML\js混杂的时候,你会有疑问:为什么有这么多东西要重复呢?
所以一般我们把一些简单的操作独立出来,写成类或者函数放进包含文件(include)。

那么以上的操作我们可以使用两个文件来实现:

conn.asp

以下为引用的内容:
<%
db_path = "database/cnbruce.mdb"
Set conn= Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath(db_path)
conn.Open connstr
%>

showit.asp

以下为引用的内容:
<!--#include file="conn.asp" -->
<%
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from cnarticle"
rs.Open sql,conn,1,1
if rs.EOF and rs.BOF then
response.write ("暂时还没有文章")
else
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
end if
rs.close
Set rs = Nothing
conn.close
set conn=Nothing
%>

参考:http://www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=448

现在相对简单多了,如果有多个操作页面我们只要导入连接文件就可以了,不过还是不够简洁,哪里不简洁?
一直在创建server,一直在写close,这样很容易出错,并且看起来与内容无关的太多。

那我再改进下:

把conn.asp文件改成:

以下为引用的内容:
<%
Dim Conn
Dim Rs
Sub CloseDatabase
    Conn.close
    Set Conn = Nothing
End Sub
Sub OpenDatabase
    Dim StrServer,StrUid,StrSaPwd,StrDbName
    StrServer="192.168.1.1"        '数据库服务器名
    StrUid="sa"            '您的登录帐号
    StrSaPwd=""            '您的登录密码
    StrDbName="cnbruce.mdb"        '您的数据库名称
        Set Conn = Server.CreateObject("ADODB.Connection")
        '用于连接ACCESS
        Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath(StrDbName)
        '用于连接MSSQL
        'Conn.ConnectionString = "Driver={sql server};driver={SQL server};server="&StrServer&";uid="&StrUid&";pwd="&StrSaPwd&";database=
"&StrDbName
        set rs=server.CreateObject("ADODB.RecordSet")
        conn.open
        if Err Then
            err.Clear
            Set Conn = Nothing
            GBL_CHK_TempStr = GBL_CHK_TempStr & "数据库连接错误!"
            Response.Write GBL_CHK_TempStr
            Response.End
        End If   
End Sub
%>

现在我们的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 DataTable
    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 DataTable
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 DataTable
Num=loadData.SelectDataNum(sql)
%>

来取得记录条数,可以用于分页或者用户名是否重复的判断。

其它的对数据记录的操作我们新建一个类,使用UpdateTable来完成操作:

以下为引用的内容:
<%
Class DataTable
    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 DataTable
UpdateDate.UpdataSql(sql)
%>

当然你也这以这样写:

以下为引用的内容:
<%
sql="insert into cnarticle(cn_title,cn_author,cn_content) values(' "&whattitle&" ',' "&whoauthor&" ',' "&whatcontent&" ')"
opendatabase
conn.execute(sql)
closedatabase
%>

考虑到可能删除语句我们会这么写:
sql="delect from cnarticle where id in(1,3,5,6,7,8)"

我新建一个类DeldataTable,直接使用DeldataTable.DeldataSql(tableName,DelField,id)完成记录的删除操作。

以下为引用的内容:
<%
Class DataTable
    dim tempvalue
    public Function DeldataSql(tableName,DelField,id)
        If tableName<>"" and id<>"" then
            sql="delete from "&tableName
            If isnumeric(id) and instr(id,",")=0 then
                sql = sql & " where "&DelField&" = "&id
            Else
                sql = sql & " where "&DelField&" in ("& id &")"
            End If
            Opendatabase
                conn.execute(sql)
            Closedatabase
            tempvalue=true
        Else
            tempvalue=false
        End If
        DeldataSql=tempvalue
    End Function
End Class
%>

以下是我的sql.asp文件,请自己进行增删

以下为引用的内容:

<%
'用于查询数据
Class DataTable
    '查出记录
    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
    '查出记录条数
    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
    '使用select count(*) from tablename 查出记录有数
    public Function SelectCountNum(sql)
        If sql<>"" then
            Opendatabase
            Rs.open sql,conn,1,1
            If not Rs.eof then
                Thedata=Rs.GetRows(-1)
                Closedatabase
                Num=Thedata(0,0)
            Else
                Closedatabase
            End If
        End If
        SelectCountNum=Num
    End Function
    '将查询的数据全部生成隐藏值
    public Function GetHiddenData(sql)
        dim tempvalue
        If sql<>"" then
            Opendatabase
            Rs.open sql,conn,1,1
            If not Rs.eof then
                Thedata=Rs.getRows(-1)
                TheFieldCount=rs.fields.count
                For i=0 to TheFieldCount-1
                    TheFieldList = TheFieldList & Rs.fields(i).name & ","
                Next
                Closedatabase
                TheField = split(TheFieldList,",")
                For i=0 to TheFieldCount-1
                    tempvalue = tempvalue & "<input type=""hidden"" id="""&TheField(i)&""" name="""&TheField(i)&""" value="""&Thedata(i,0)&""" />"
                Next
            Else
                Closedatabase
            End If
        End If
        GetHiddenData=tempvalue
    End Function
    public Function UpdataSql(sql)
        If sql<>"" then
            Opendatabase
            conn.execute(sql)
            Closedatabase
        End If
    End Function
   
    public Function DeldataSql(tableName,DelField,id)
        dim tempvalue
        If tableName<>"" and id<>"" then
            sql="delete from "&tableName
            If isnumeric(id) and instr(id,",")=0 then
                sql = sql & " where "&DelField&" = "&id
            Else
                sql = sql & " where "&DelField&" in ("& id &")"
            End If
            Opendatabase
                conn.execute(sql)
            Closedatabase
            tempvalue=true
        Else
            tempvalue=false
        End If
        DeldataSql=tempvalue
    End Function
End Class
%>