电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> 数据库
一个通用数据库显示程序,能调任意库,任意字段,多关键字搜索,自动分页
作者:网友供稿 点击:9
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
.

阿余经常写一些数据库相关的程序,当然离不开显示库中的数据了,说实话,做这样的程序真是无聊啊,所以,阿余就想写个函数,一个通用的数据库显示函数.要求如下:

1. 能显示指定的字段,当然,字段名和显示的文字可以不一样.
2. 能同时按多个字段进行查询,支持模糊和精确两种查询方式.
3. 有横向排列和纵向排列字段两种显示方式.
4. 能自动分页.
5. 能设定每页显示多少条记录.好啦,要求大至就是这样了.根据这个要求,阿余写了下面的函数.
实际上,这里阿余写了两个函数.一个是tabdisp(),用于显示一个指定表中的内容.还有一个是er()一个小小的错误处理函数.
这个函数有5个参数(真够多的).并且各个参数都要输入数据,不能是空白,不然会出错的.
1(tab), 要调用的表名.
2(dispfild), 要显示表中的哪一些字段,如字段名和要显示的字符不一样用<分隔.比如某一个字段名为:img,但要在页面上显示为:图片, 那么,就写为img<图片,如果有多个字段,各字段间用“,”分隔,唉,老声长谈.
3(findfild), 要按哪些字段进行查询,字段名和要显示的字符间用<分隔.
4(pagen), 每页显示多少条记录.
5(fh), 显示的方向.当为s时纵向显示.为h时横向显示.
函数的返回值,当没有出错的时候,返回值为真,否则为假.
这个函数中没有包括连接到数据库的部分.所以要自己连接到库.另外,为了重复应用.可以把这个函数专门用一个文件来保存,然后在需要的时候包含这个文件.
看下面的函数: (不要不看完,最后我举有应用的例子)

<%
以下定义一个显示表格的函数tabdisp(),参数tab,被显示的表名,dispfild,要显示的字段,findfild,查询字段,pagen,每页显示数量,fh,显示的方向
如显示成功,返回值为真,不成功,返回值为假.
多个字段间用,分隔
查询字段一定要包含在显示字段中.
如字段名与要显示的名称不一样,用<分隔,前面是字段名,后面是显示的名字,而要进行搜索的字段则一定要用<分隔要字段名和显示字符
fh显示方向为"h"时作横向显示,为"s"时作纵向显示 区分大小写
function  tabdisp(tab,dispfild,findfild,pagen,fh)
  on error resume next
  if dispfild="" then dispfild="*"
  if pagen="" then pagen=15
  dispfild1=dispfild&","
  findfild1=findfild&","

  dim findl(10),findr(10)
  i1=1
  do while instr(findfild1,",")<>0
    star=instr(findfild1,",")
    findl(i1)=left(findfild1,star-1)
    findr(i1)=findl(i1)
    fildbak=findl(i1)
    findl(i1)=left(fildbak,instr(fildbak,"<")-1)
    findr(i1)=right(fildbak,len(fildbak)-instr(fildbak,"<"))
    findfild1=right(findfild1,len(findfild1)-star)
    i1=i1+1
  loop
  
  response.write "<table border=0 width=100% cellspacing=0><tr><form method=get>"
  response.write "<td > "  width=50%
  if session("flmenulbup")<>"" then
    response.write "当前栏目:<a href=flmen.asp?menulb="&session("flmenulbup")&">"&mid(session("flmenulbup"),4,200)&"</a>"
  end if
  if session("flmenulb")<> session("flmenulbup")  then
    response.write " >> <a href=flmen.asp?menulb="&session("flmenulbup")&"&lb="&session("flmenulb")&">"&mid(session("flmenulb"),4,200)&"</a>"
  end if
  response.write "</td><td align=right ></td><td  align=right><table border=0 cellspacing=0 cellpadding=0> "
  response.write "<tr><td rowspan=2><img src=img/search.gif width=50>   </td>"
  for i3=1 to i1-1
     response.write "<td>"&findr(i3)&":</td>"
  next
  response.write  "</tr><tr>"  <td></td>
  for i2=1 to i1-1
     response.write "<td><input type=text name=key_word"&i2&" size=8 value=></td>"
  next

  response.write "<tr></table></td><td width=20%> <input type=checkbox name=find_fr value=yes>精确 <input type=submit value=开始 name=b_find></td>"
  response.write "</form> </tr></table>"
  find_fr=request("find_fr")
  tj=""
  urllr="&find_fr="&find_fr
  findlrdisp=""
  for i3=1 to i1-1
     tj1=request("key_word"&i3)
     urllr=urllr&"&key_word"&i3&"="&tj1
     if tj1=findr(i3) or tj1="" then
       tj1=""
     else
        if find_fr<>"yes" then
          findlrdisp=findlrdisp&findr(i3)&"包含“"&tj1&"” "
          tj1=" and "&findl(i3)&" like %"&trim(tj1)&"% "
        else
           findlrdisp=findlrdisp&findr(i3)&"是“"&tj1&"” "
           tj1=" and "&findl(i3)&" = "&trim(tj1)&" "
        end if
     end if
     tj=tj&tj1
     if tj="" then
        topsn=" top 300 "
     else
        topsn=""
     end if
  next

  dim fild(35),dispfil(35),dispfildlr
  dispfildlr=""
  i=1
  dispfild1=dispfild1
  do while instr(dispfild1,",")<>0
  star=instr(dispfild1,",")
  fild(i)=left(dispfild1,star-1)
  dispfil(i)=fild(i)
  if instr(fild(i),"<")<>0 then
      fildbak=fild(i)
      fild(i)=left(fildbak,instr(fildbak,"<")-1)
      dispfil(i)=right(fildbak,len(fildbak)-instr(fildbak,"<"))
  end if
  dispfildlr=dispfildlr&fild(i)&","
  dispfild1=right(dispfild1,len(dispfild1)-star)
  i=i+1
  loop
  dispfildlr=left(dispfildlr,len(dispfildlr)-1)
   sql="select  "&topsn&"  id,"&dispfildlr&"    from "&tab&"  where id<>-1 "&tj&"  order by -id"
    response.write sql
    session("tabsql")=sql
    set rs=server.createobject("adodb.recordset")
    rs.open sql,conn,1,3
    if not rs.eof then
    pagesn=request("pagesn")
    if pagesn<=0 or pagesn="" then pagesn=1
    rs.pagesize=pagen
    pagezs=rs.pagecount
    if cint(pagesn)>pagezs then pagesn=pagezs
       zs=rs.recordcount
       page=(pagesn-1)*pagen   
       rs.move page,1
       if findlrdisp<>"" then response.write "经搜索,"&findlrdisp&"的记录如下:"
       response.write  "<table border=0 width=90%>"
       if zs>=300 and topsn<>"" then
          response.write  "<tr>  <form method=post   action=?"&urllr&"><td width=54%>总数很多,当前显示前"&zs&"条,分"&pagezs&"页,现在是第"&pagesn&"页</td>"
       else
          response.write  "<tr>  <form method=post   action=?"&urllr&"><td width=54%>一共"&zs&"条,分"&pagezs&"页,现在是第"&pagesn&"页</td>"
       end if
       response.write  "<td width=11% align=center><a href=?pagesn="&pagesn-1&urllr&">上一页</a></td>"
       response.write  "<td width=11% align=center><a href=?pagesn="&pagesn+1&urllr&">下一页</a></td>"
       response.write  "<td width=24% align=right>跳转到第 "
       response.write  "<input type=text name=pagesn size=2>页<input type=submit value=go name=b1></td></form> "
       response.write  "</tr></table> "
       if fh="h" then
       response.write  " <table border=0 width=100% cellspacing=1 cellpadding=0>"
       response.write  " <tr bgcolor=#edbaa5 align=center> "
       for ii=1 to i-1
          response.write  "<td>"&dispfil(ii)&"</td>"
       next
       response.write  "</tr>"
       for iii=1 to pagen
         response.write "<tr  bgcolor=#fffaee>"
           for ii=1 to i-1
             response.write "<td>"&rs(fild(ii))&"</td>"
           next
          response.write "</tr>"
        rs.movenext
        if rs.eof then exit for
      next
      response.write "</table>"
    else
       response.write  " <table border=0 width=100% cellspacing=1 cellpadding=0>"
       for iii=1 to pagen
           for ii=1 to i-1
             response.write "<tr><td bgcolor=#edbaa5 align=right width=20%>"&dispfil(ii)&":</td><td bgcolor=#fffaee>"&rs(fild(ii))&"</td></tr>"
           next
           response.write "<tr  height=1><td bgcolor=#cfba9e  colspan=2 align=right width=20%> </td></tr>"
        rs.movenext
        if rs.eof then exit for
      next
      response.write "</table>"
    end if
  rs.close
  response.write  "<table border=0 width=90%>"
if zs>=300 and topsn<>"" then
  response.write  "<tr>  <form method=post   action=?"&urllr&"><td width=54%>总数很多,当前显示前"&zs&"条,分"&pagezs&"页,现在是第"&pagesn&"页</td>"
else
  response.write  "<tr>  <form method=post   action=?"&urllr&"><td width=54%>一共"&zs&"条,分"&pagezs&"页,现在是第"&pagesn&"页</td>"
end if
  response.write  "<td width=11% align=center><a href=?pagesn="&pagesn-1&urllr&">上一页</a></td>"
  response.write  "<td width=11% align=center><a href=?pagesn="&pagesn+1&urllr&">下一页</a></td>"
  response.write  "<td width=24% align=right>跳转到第 "
  response.write  "<input type=text name=pagesn size=2>页<input type=submit value=go name=b1></td></form> "
  response.write  "</tr></table> "
  else
      response.write  "没有找到合适的记录"
  end if
if er() then
  tabdisp=false
else
  tabdisp=true
end if
end function

function er()  错误处理函数
        if err.number = 0 then
        er = false
        else
        err.clear
        er = true
        end if
end function
%>

    终于看完函数了....
各位有点累,其实仔不仔细看都没有关系.反正只管调它就是了.下面举一个应用的例子.
首先, 我们把前面的函数就是<%和%>之间的部分保存到一个叫 tabdisp.asp的文件中.
代码如下:
显示一个职式名册表,表名: zg
有id 姓名 职务 职称 tel bp dz(地址)几个字段.

<%
。。。。。。
连接数据库,此处略过,不写了。
%>
<!--#include file="tabdisp.asp"-->
<%
disp="姓名,职务,职称,tel<电话,bp<传呼,dz<地址"
find="姓名<姓名,职务<职务,dz<地址"
pagen="15"
fh="h"
tab="zg"

if not tabdisp(tab,disp,find,pagen,fh) then
    response.write "出现错误,"
end if
set conn=nothing
%>

把上面的内容随便起个文件名,就一切ok.

另外, 阿余的站www.zydn.net有不少好文章给大家.阿余也在站上随时准备和和朋友交流.阿余的email:coolkk@21cn.com
还有啊,软件世界竟然发了篇牛记的文章,说程序员工资太高,阿余在站上也放了个论坛,大家可以一起到阿余的站上去骂死那个家伙.
文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·数据库开发个人总结(ADO.NET小结)-.NET教程,数据库应用
·怎么由DataSet将数据导入Excel?-.NET教程,数据库应用
·动态创建SQL Server数据库、表、存储过程-ASP教程,数据库相关
·Win32环境下动态链接库(DLL)编程原理-.NET教程,数据库应用
·封装的ADO.NET对数据库操作经典类-.NET教程,数据库应用
·在DataGridView中获得DataGridViewCheckBoxColumn的状态-ASP教程,数据库相关
·DataGrid使用心得(附大量代码)-ASP教程,数据库相关
·用代码创建DataGrid的多链接及checkbox事件响应-.NET教程,数据库应用
·ADO.NET 的最佳实践技巧-.NET教程,数据库应用
·转载: 用纯ASP代码实现图片上传并存入数据库中

最新文章
·根据数据表中数据,生成Powerpoint幻灯片-ASP教程,数据库相关
·DataGrid中的按钮反选事件与NamingContainer(命名容器)-downmoon-ASP教程,数据库相关
·使用用VB处理MYSQL数据库中二进制数据问题-.NET教程,VB.Net语言
·关于DataGridView中如何接收处于编辑状态下的当前信息-ASP教程,数据库相关
·在DataGridView中获得DataGridViewCheckBoxColumn的状态-ASP教程,数据库相关
·.net下访问Access数据库需要注意的问题-.NET教程,Asp.Net开发
·ActiveMQ4.1+Spring2.0的POJO JMS方案(上)-.NET教程,数据库应用
·ASP.NET 2.0中直接将Access数据库导入到Excel文件中-.NET教程,Asp.Net开发
·NET(C#)连接各类数据库-集锦-.NET教程,C#语言
·ASP.NET2.0连接SQL Server数据库详解-.NET教程,Asp.Net开发




版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号