欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【ASP快速开发方法之数据操作】,下面是详细的分享!
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 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 %> |
<%
'用于查询数据
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
%>
以上所分享的是关于ASP快速开发方法之数据操作,下面是编辑为你推荐的有价值的用户互动:
相关问题:快速开发软件工具有什么?只需要能够实现对数据库...
答:如果没理解错的话,你是想要个可视化SQL编写+数据浏览编辑工具,最符合你的需要的是PowerBuilder,不过你还是要学一些编程和数据库理论,这件事没你想的那么简单。 另外,数据库本身的集成管理环境应该也有部分工具可以实现你的部分想法。 现在... >>详细
相关问题:ASP.NET数据库操作
答:DataSet中的DataTable就可以。从数据库中查询出来的结果集一般都放到DataSet,假设为ds,那么ds.Table[0]就是一个DataTable,假设赋值给dt,那么对于dt是有很多方法可以操作,比如使用dt.Rows属性选择行,或使用dt.Columns属性选择列,或者使用d... >>详细
相关问题:ASP.NET技术如何快速开发符合项目需求的网站,如何...
答:设置个字典表,存放公用数据 例如生活信息平台 http://www.naoqiu.com 数据库也是设计个子电表存放公用数据 >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
