asp快速开发方法之数据操作
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
[p]我的目的是让开发变得简单,尽可能少地考虑实现语句,更多地把精力用于思考业务逻辑。希望我的文章对大家有所启发和帮助。[/p]
[p]先看以下例子:[/p] <%[br]db_path = "database/cnbruce.mdb"[br]set conn= server.createobject("adodb.connection")[br]connstr = "provider=microsoft.jet.oledb.4.0;data source="&server.mappath(db_path)[br]conn.open connstr[br]set rs = server.createobject ("adodb.recordset")[br]sql = "select * from cnarticle"[br]rs.open sql,conn,1,1[br]if rs.eof and rs.bof then[br]response.write ("暂时还没有文章")[br]else[br]do until rs.eof[br]response.write("文章标题是:"& rs("cn_title"))[br]response.write(" 文章作者是:"& rs("cn_author"))[br]response.write(" 文章加入时间是:"& rs("cn_time"))[br]response.write(" 文章内容是:"& rs("cn_content"))[br]response.write(" ")[br]rs.movenext[br]loop[br]end if[br]rs.close [br]set rs = nothing[br]conn.close [br]set conn=nothing[br]%> [p][br]嗯,这是一个典型的读取数据并显示的例子,参见:[url=http://cnbruce.com/blog/showlog.asp?cat_id=26&log_id=448]http://cnbruce.com/blog/showlog.asp?cat_id=26&log_id=448[/url][br]嗯,确实简单。从上至下,很容易明白。但是当你对多个表进行读插删改的时候,当你的代码里有很多html\js混杂的时候,你会有疑问:为什么有这么多东西要重复呢?[br]所以一般我们把一些简单的操作独立出来,写成类或者函数放进包含文件(include)。[br][br]那么以上的操作我们可以使用两个文件来实现:[br]conn.asp[/p] <%[br]db_path = "database/cnbruce.mdb"[br]set conn= server.createobject("adodb.connection")[br]connstr = "provider=microsoft.jet.oledb.4.0;data source="&server.mappath(db_path)[br]conn.open connstr[br]%> [p][br]showit.asp[/p] [br]<%[br]set rs = server.createobject ("adodb.recordset")[br]sql = "select * from cnarticle"[br]rs.open sql,conn,1,1[br]if rs.eof and rs.bof then[br]response.write ("暂时还没有文章")[br]else[br]do until rs.eof[br]response.write("文章标题是:"& rs("cn_title"))[br]response.write(" 文章作者是:"& rs("cn_author"))[br]response.write(" 文章加入时间是:"& rs("cn_time"))[br]response.write(" 文章内容是:"& rs("cn_content"))[br]response.write(" ")[br]rs.movenext[br]loop[br]end if[br]rs.close [br]set rs = nothing[br]conn.close [br]set conn=nothing[br]%> [p][br]现在相对简单多了,如果有多个操作页面我们只要导入连接文件就可以了,不过还是不够简洁,哪里不简洁?[br]一直在创建server,一直在写close,这样很容易出错,并且看起来与内容无关的太多。[br][br]那我再改进下:[br]把conn.asp文件改成:[/p] [p][/p] <%[br]dim conn[br]dim rs[br]sub closedatabase[br] conn.close[br] set conn = nothing[br]end sub[br]sub opendatabase[br] dim strserver,struid,strsapwd,strdbname[br] strserver="192.168.1.1" '数据库服务器名[br] struid="sa" '您的登录帐号[br] strsapwd="" '您的登录密码[br] strdbname="cnbruce.mdb" '您的数据库名称[br] set conn = server.createobject("adodb.connection")[br] '用于连接access[br] conn.connectionstring = "provider=microsoft.jet.oledb.4.0; data source=" & server.mappath(strdbname)[br] '用于连接mssql[br] 'conn.connectionstring = "driver={sql server};driver={sql server};server="&strserver&";uid="&struid&";pwd="&strsapwd&";database="&strdbname[br] set rs=server.createobject("adodb.recordset")[br] conn.open[br] if err then[br] err.clear[br] set conn = nothing[br] gbl_chk_tempstr = gbl_chk_tempstr & "数据库连接错误!"[br] response.write gbl_chk_tempstr[br] response.end[br] end if [br]end sub[br]%> 现在我们的showit.asp可以这样写:[br]showit.asp[br] [br]<%[br]sql = "select * from cnarticle"[br]opendatabase[br]rs.open sql,conn,1,1[br]if not rs.eof then[br] do until rs.eof[br] response.write("文章标题是:"& rs("cn_title"))[br] response.write(" 文章作者是:"& rs("cn_author"))[br] response.write(" 文章加入时间是:"& rs("cn_time"))[br] response.write(" 文章内容是:"& rs("cn_content"))[br] response.write(" ")[br] rs.movenext[br] loop[br]else[br] response.write ("暂时还没有文章")[br]end if[br]closedatabase[br]%>[br]嗯,我们又少写了一些东西,这样是最简单的吗?当然不是!还可以更简单。[br]使用getrows把查询出来的数据传给一个变量,使用ubound方法取得数据记录条数。[br]不明白?没关系,让我们继续往下看:[br][br]再建个文件:sql.asp[br]sql.asp[br] <%[br]class selectdatatable[br] public function selectdata(sql)[br] if sql<>"" then[br] opendatabase[br] rs.open sql,conn,1,1[br] if not rs.eof then[br] thedata=rs.getrows(-1)[br] closedatabase[br] else[br] closedatabase[br] end if[br] end if[br] selectdata=thedata[br] end function[br]end class[br]%>[br]嗯,复制它就可以了,现在我们的showit.asp可以简单地这样写:[br][br]showit.asp[br] [br][br]<%[br]sql = "select * from cnarticle"[br]set loaddata=new selectdatatable[br]thedata=loaddata.selectdata(sql)[br]if isarray(thedata) then[br] num=ubound(thedata,2)[br] for i=0 to num[br] response.write("文章标题是:"& thedata(1,i))[br] response.write(" 文章作者是:"& thedata(2,i))[br] response.write(" 文章加入时间是:"& thedata(3,i))[br] response.write(" 文章内容是:"& thedata(4,i))[br] response.write(" ")[br] next[br]else[br] response.write("暂时还没有文章")[br]end if[br]%>[br]呵呵,这样,我们只要用两句语句就完成了数据的读取。同样的,通过在sql.asp中加入[br] <%[br] public function selectdatanum(sql)[br] if sql<>"" then[br] opendatabase[br] rs.open sql,conn,1,1[br] if not rs.eof then[br] thedata=rs.getrows(-1)[br] closedatabase[br] num=ubound(thedata,2)[br] else[br] closedatabase[br] end if[br] end if[br] selectdatanum=num[br] end function[br]%>[br]我们就可以使用[br] <%[br]sql = "select * from cnarticle"[br]set loaddata=new selectdatatable[br]num=loaddata.selectdatanum(sql)[br]%>[br]来取得记录条数,可以用于分页或者用户名是否重复的判断。[br][br]其它的对数据记录的操作我们新建一个类,使用updatetable来完成操作:[br] <%[br]class updatatable[br] public function updatasql(sql)[br] if sql<>"" then[br] opendatabase[br] conn.execute(sql)[br] closedatabase[br] end if[br] end function[br]end class[br]%>[br] <%[br]sql = "delete from cnarticle"[br]set updatedate=new updatatable[br]updatedate.updatasql(sql)[br]%> 当然你也这以这样写:[br] <%[br]sql="insert into cnarticle(cn_title,cn_author,cn_content) values(' "&whattitle&" ',' "&whoauthor&" ',' "&whatcontent&" ')"[br]opendatabase[br]conn.execute(sql)[br]closedatabase[br]%>[br]考虑到可能删除语句我们会这么写:[br]sql="delect from cnarticle where id in(1,3,5,6,7,8)"[br][br]我新建一个类deldatatable,直接使用deldatatable.deldatasql(tablename,delfield,id)完成记录的删除操作。[br] <%[br]class deldatatable[br] dim tempvalue[br] public function deldatasql(tablename,delfield,id)[br] if tablename<>"" and id<>"" then[br] sql="delete from "&tablename[br] if isnumeric(id) and instr(id,",")=0 then[br] sql = sql & " where "&delfield&" = "&id[br] else[br] sql = sql & " where "&delfield&" in ("& id &")"[br] end if[br] opendatabase[br] conn.execute(sql)[br] closedatabase[br] tempvalue=true[br] else[br] tempvalue=false[br] end if[br] deldatasql=tempvalue[br] end function[br]end class[br]%>[br]以下是我的sql.asp文件,请自己进行增删[br]复制代码 <%[br]'用于查询数据[br]class selectdatatable[br] '查出记录[br] public function selectdata(sql)[br] if sql<>"" then[br] opendatabase[br] rs.open sql,conn,1,1[br] if not rs.eof then[br] thedata=rs.getrows(-1)[br] closedatabase[br] else[br] closedatabase[br] end if[br] end if[br] selectdata=thedata[br] end function[br] '查出记录条数[br] public function selectdatanum(sql)[br] if sql<>"" then[br] opendatabase[br] rs.open sql,conn,1,1[br] if not rs.eof then[br] thedata=rs.getrows(-1)[br] closedatabase[br] num=ubound(thedata,2)[br] else[br] closedatabase[br] end if[br] end if[br] selectdatanum=num[br] end function[br] '使用select count(*) from tablename 查出记录有数[br] public function selectcountnum(sql)[br] if sql<>"" then[br] opendatabase[br] rs.open sql,conn,1,1[br] if not rs.eof then[br] thedata=rs.getrows(-1)[br] closedatabase[br] num=thedata(0,0)[br] else[br] closedatabase[br] end if[br] end if[br] selectcountnum=num[br] end function[br] '将查询的数据全部生成隐藏值[br] public function gethiddendata(sql)[br] dim tempvalue[br] if sql<>"" then[br] opendatabase[br] rs.open sql,conn,1,1[br] if not rs.eof then[br] thedata=rs.getrows(-1)[br] thefieldcount=rs.fields.count[br] for i=0 to thefieldcount-1[br] thefieldlist = thefieldlist & rs.fields(i).name & ","[br] next[br] closedatabase[br] thefield = split(thefieldlist,",")[br] for i=0 to thefieldcount-1[br] tempvalue = tempvalue & ""[br] next[br] else[br] closedatabase[br] end if[br] end if[br] gethiddendata=tempvalue[br] end function[br] [br]end class[br]class updatatable[br] public function updatasql(sql)[br] if sql<>"" then[br] opendatabase[br] conn.execute(sql)[br] closedatabase[br] end if[br] end function[br] [br]end class[br]class deldatatable[br] dim tempvalue[br] public function deldatasql(tablename,delfield,id)[br] if tablename<>"" and id<>"" then[br] sql="delete from "&tablename[br] if isnumeric(id) and instr(id,",")=0 then[br] sql = sql & " where "&delfield&" = "&id[br] else[br] sql = sql & " where "&delfield&" in ("& id &")"[br] end if[br] opendatabase[br] conn.execute(sql)[br] closedatabase[br] tempvalue=true[br] else[br] tempvalue=false[br] end if[br] deldatasql=tempvalue[br] end function[br]end class[br]%> 该文章在 2010/7/3 13:37:46 编辑过 |
关键字查询
相关文章
正在查询... |