end if
'断开数据库
set rs=nothing
conn.close
set conn=nothing
%>
理解上述程序时,着重琢磨核心部分,8组语句一一对应了3个搜索框中的8种状态
|
Name |
Tel |
School |
|
空 |
空 |
空 |
|
空 |
空 |
非空 |
|
空 |
非空 |
空 |
|
空 |
非空 |
非空 |
|
非空 |
空 |
空 |
|
非空 |
空 |
非空 |
|
非空 |
非空 |
空 |
|
非空 |
非空 |
非空 |
另外trim()是VB的函数,将输入的字符串前后的空格去掉;%是SQL语言中的多字符通配符(_是单字符通配符),由此可见%"&trim()&"%对搜索框中输入的关键字是分别向左向右匹配的;SQL语言中用and连接说明非空条件之间是“与”关系。
再来看看递进法,与枚举法相比它们只有核心部分不同:
'递进法的搜索核心,依次判断条件为空否,非空则将其加入搜索条件
sql="select * from address where"
if Name<>"" then
sql=sql&" Name like '%"&Name&"%' "
flag=1
end if
if Tel<>"" and flag=1 then
sql=sql&" and Tel like '%"&Tel&"%'"
flag=1
elseif Tel<>"" then
sql=sql&" Tel like '%"&Tel&"%'"
flag=1
end if
if Company<>"" and flag=1 then
sql=sql&" and Company like '%"&Company&"%'"
flag=1
elseif Company <>"" then
sql=sql&" Company like '%"&Company&"%'"
flag=1
end if
if flag=0 then
sql="select * from address order by ID asc"
end if
rs.open sql,conn,1,1
递进法是一个明智的算法,单从语句的长短就可以看出来了。这个算法的难点和精髓就在flag和&上。首先你应该清楚&在SQL中就是一个字符串连接符,把该符号左右的字符拼接在一起。再回到程序,当Name不为空时sql="select * from address where Name like '%"&Name&"%' "同时flag=1;接下来当Name不为空时且Tel不为空时,即Tel<>"" and flag=1时,sql="select * from address where Name like '%"&Name&"%' and Tel like '%"&Tel&"%' "同时flag=1,否则当Name为空Tel不为空,sql="select * from address where Tel like '%"&Tel&"%' "同时flag=1;以此类推就可以推广到n个条件的搜索。当然条件皆为空时,即flag=0将选择所有表中所有项。
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




