多条件查询代码(同时生成分页转向url)

来源:互联网 发布:数据可视化界面设计 编辑:程序博客网 时间:2024/05/29 08:23

程序编写过程中经常用到多条件查询,如果条件很多,处理起来是不是会很麻烦?
查询完后要分页,点“下一页”后查询参数没了?
下面这种方法可以很好的解决这个问题,使用起来也比较方便。
保存下面的代码到example.asp,然后运行example.asp文件,就会看到效果,
程序会自动保存提交的查询参数,即使翻页也不会丢失!

 

<%

'获取页面传递过来的参数
dim KeyWords,UserType,UserFrom,UserIsLock,UsefulLife,CorpType,choose '定义变量
KeyWords =trim(request.querystring("KeyWords"))
UserType =trim(request.querystring("UserType"))
UserFrom =trim(request.querystring("UserFrom"))
UserIsLock =trim(request.querystring("UserIsLock"))
UsefulLife =trim(request.querystring("UsefulLife"))
CorpType =trim(request.querystring("CorpType"))
'获取页面传递过来的参数结束

 

'查询结果
page=cint(request.querystring("page"))  '获取页面传递过来的分页参数
if  page=""  or  page<1  then  page=1      '处理传递过来的分页参数

dim  vs(5) '定义数组,本数组用来存储url参数
vs(0)="":vs(1)="":vs(2)="":vs(3)="":vs(4)="" '初始化

 

'根据条件开始给数组元素赋值,以确定查询参数
if  KeyWords<>"" and KeyWords<>"输入会员用户名搜索" then  vs(0)="KeyWords="  &  KeyWords
if  UserType<>""  then  vs(1)="UserType="  &  UserType
if  UserFrom<>""  then  vs(2)="UserFrom="  &  UserFrom
if  UserIsLock<>""  then  vs(3)="UserIsLock="  &  UserIsLock
if  UsefulLife<>""  then  vs(4)="UsefulLife="  &  UsefulLife
if  CorpType<>""  then  vs(5)="CorpType="  &  CorpType


'赋值结束

dim  conStr(6) '定义数组,本数组存储SQL查询条件
conStr(0)="":conStr(1)="":conStr(2)="":conStr(3)="":conStr(4)="":conStr(5)="" '初始化

 

'根据条件开始给数组元素赋值,以确定查询条件
if  KeyWords<>"" and KeyWords<>"输入会员用户名搜索"  then    conStr(0)="[UserName]='"  &  KeyWords  &  "'"
if  UserType<>"-1"  and  UserType<>""  then  conStr(1)="[UserType]="  &  UserType

if  UserFrom<>"-1"  and  UserFrom<>""  then  conStr(2)="[UserFrom]="  &  UserFrom

if  UserIsLock<>"-1"  and  UserIsLock<>""  then  conStr(3)="[UserIsLock]="  &  UserIsLock
if  UsefulLife<>"-1"  and  UsefulLife<>""  then  conStr(4)="[UsefulLife]="  &  UsefulLife
if  CorpType<>"-1"  and  CorpType<>""  then  conStr(5)="[CorpType]="  &  CorpType


'赋值结束

dim  SQLSTR:SQLSTR="" '定义变量,存储组合后的SQL查询条件
dim  sendSTR:sendSTR="" '定义变量,存储组合后的url参数

 

'循环赋值
for i=0 to 5 
        if conStr(i)<>"" then
                if SQLSTR="" then                       
                        SQLSTR=" where " & conStr(i)        '使用此条件来添加where关键字
                else
                        SQLSTR=SQLSTR & " and " & conStr(i)        '关键字and来连接查询条件
                end if
        end if
        if vs(i)<>"" then
                sendSTR=sendStr & vs(i) & "&"                        '直接用&来链接各url参数
        end if
next

 

set rs=server.CreateObject("adodb.recordset")
SQL="Select  *  from  [Users]"  &  SQLSTR  &  "  order  by  [UserID]  desc" '生成查询SQl
rs.open SQL,conn,1,1
s_url="example?"  &  sendStr '生成分页转向url,注意example.asp后面的?,一定不要漏了
response.write  "<br  /><br  />生成的SQL查询语句:"  &  SQL
response.write  "<br  /><br  />生成的分页转向url:"  &  s_url
response.write  "<br  /><br  />当前在第"  &  page  &  "页!"
response.write  "<br  /><br  /><a  href="""  &  s_url  &  "page="  &  page-1  &  """>上一页</a>  <a  href="""  &  s_url  &  "page="  &  page+1  &  """>下一页</a>"
%>
<table class="tableBorder" cellspacing="1" cellpadding="3" width="96%" align="center" border="0">
 <tr>
  <th height="25" align="left">会员列表</th>
 </tr>
 <tr>
  <td class="TableRow1">
  <!--提交表单开始-->
  <form style="display:inline;" name="search" action="" method="get">  <!--注意,必须用get方法提交表单-->
   <input id="KeyWords" name="KeyWords" <% if KeyWords<>"" then %> value="<%=KeyWords%>" <% else %> value="输入会员用户名搜索" <% end if %> onFocus="if(this.value=='输入会员用户名搜索')this.value='';" onBlur="if(this.value=='')this.value='输入会员用户名搜索';" style="color:gray;" />
   <select name="UserType" style="vertical-align:middle;">
    <option value="-1" <%if  UserType="-1" then response.write "selected"%>>不限会员类型</option>
    <option value="1" <%if UserType="1" then response.write "selected"%>>金卡会员</option>
    <option value="2" <%if UserType="2" then response.write "selected"%>>银卡会员</option>
   </select>
   <select name="UserFrom" style="vertical-align:middle;">
    <option value="-1" <%if UserFrom="-1" then response.write "selected"%>>不限添加方式</option>
    <option value="1" <%if  UserFrom="1" then response.write "selected"%>>前台注册</option>
    <option value="0" <%if UserFrom="0" then response.write "selected"%>>后台添加</option>
   </select>
   <select name="UserIsLock" style="vertical-align:middle;">
    <option value="-1" <%if UserIsLock="-1" then response.write "selected"%>>不限会员状态</option>
    <option value="1" <%if  UserIsLock="1" then response.write "selected"%>>激活</option>
    <option value="0" <%if UserIsLock="0" then response.write "selected"%>>锁定</option>
   </select>
   <select name="UsefulLife" style="vertical-align:middle;">
    <option value="-1" <%if UsefulLife="-1" then response.write "selected"%>>不限有效期限</option>
    <option value="1" <%if UsefulLife="1" then response.write "selected"%>>1年</option>
    <option value="2" <%if UsefulLife="2" then response.write "selected"%>>2年</option>
    <option value="3" <%if UsefulLife="3" then response.write "selected"%>>3年</option>
    <option value="4" <%if UsefulLife="4" then response.write "selected"%>>4年</option>
    <option value="5" <%if UsefulLife="5" then response.write "selected"%>>5年</option>
   </select>
   <select name="CorpType" style="vertical-align:middle;">
    <option value="-1" <%if CorpType="-1" then response.write "selected"%>>不限企业类型</option>
    <option value="1" <%if CorpType="1" then response.write "selected"%>>经销商</option>
    <option value="2" <%if CorpType="2" then response.write "selected"%>>酒类企业</option>
    <option value="3" <%if CorpType="3" then response.write "selected"%>>包装企业</option>
    <option value="4" <%if CorpType="4" then response.write "selected"%>>机械设备企业</option>
    <option value="5" <%if CorpType="5" then response.write "selected"%>>其它企业</option>
   </select>
   <input type="submit" name="Submit2" value="开始搜索">
  </form>

<!--提交表单结束-->
  </td>
 </tr>
</table>

 

 

现在的代码是在原代码的基础上稍做了些修改,其源代码的功能没有变化。真的很佩服作者,很厉害!!

 

原帖地址:http://dev.mo.cn/article_158.html