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(" 文章作者是:"& rs("cn_author")) response.write(" 文章加入时间是:"& rs("cn_time")) response.write(" 文章内容是:"& rs("cn_content")) response.write(" ") rs.movenext loop end if rs.close set rs = nothing conn.close set conn=nothing %>
<% 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(" 文章作者是:"& rs("cn_author")) response.write(" 文章加入时间是:"& rs("cn_time")) response.write(" 文章内容是:"& rs("cn_content")) response.write(" ") rs.movenext loop end if rs.close set rs = nothing conn.close set conn=nothing %>
<% 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 <% 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(" 文章作者是:"& rs("cn_author")) response.write(" 文章加入时间是:"& rs("cn_time")) response.write(" 文章内容是:"& rs("cn_content")) response.write(" ") 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 <% 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(" 文章作者是:"& thedata(2,i)) response.write(" 文章加入时间是:"& thedata(3,i)) response.write(" 文章内容是:"& thedata(4,i)) response.write(" ") 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) %> 当然你也这以这样写: <% 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 deldatatable 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 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 '查出记录条数 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 & "" next else closedatabase end if end if gethiddendata=tempvalue end function end class class updatatable public function updatasql(sql) if sql<>"" then opendatabase conn.execute(sql) closedatabase end if end function end class class deldatatable 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 %> 该文章在 2010/7/3 13:37:46 编辑过 |
关键字查询
相关文章
正在查询... |