采用枚举法的源程序如下: <%@ CODEPAGE = "936" %> '连接数据库 <% dim conn dim DBOath dim rs dim sql Set conn=Server.CreateObject("ADODB.Connection") DBPath = Server.MapPath("addressbook.mdb") conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath Set rs=Server.CreateObject("ADODB.Recordset") '从Web页获取姓名、电话、学校的值 dim Name dim Tel dim School Name=request("Name") Tel=request("Tel") School=request("School") '枚举法的搜索核心,因为有3个条件所以要写8组If判断语句 if trim(Name)="" and trim(Tel)="" and trim(School)="" then sql="select * from address order by ID asc" end if if trim(Name)="" and trim(Tel)="" and trim(School)<>"" then sql="select * from address where School like '%"&trim(School)&"%' order by ID asc" end if if trim(Name)="" and trim(Tel)<>"" and trim(School)="" then sql="select * from address where Tel like '%"&trim(Tel)&"%' order by ID asc" end if if trim(Name)="" and trim(Tel)<>"" and trim(School)<>"" then sql="select * from address where Tel like '%"&trim(Tel)&"%' and School like '%"&trim(School)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)="" and trim(School)="" then sql="select * from address where Name like '%"&trim(Name)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)="" and trim(School)<>"" then sql="select * from address where Name like '%"&trim(Name)&"%' and School like '%"&trim(School)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)<>"" and trim(School)="" then sql="select * from address where Name like '%"&trim(Name)&"%' and Tel like '%"&trim(Tel)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)<>"" and trim(School)<>"" then sql="select * from address where Name like '%"&trim(Name)&"%' and Tel like '%"&trim(Tel)&"%' and School like '%"&trim(School)&"%' order by ID asc" end if rs.open sql,conn,1,1 '显示搜索结果 if rs.eof and rs.bof then response.write "目前通讯录中没有记录" else do while not rs.eof response.write "姓名:"&rs("Name")&"电话:"&rs("Tel")&"学校:"&rs("School")&"<br>" rs.movenext loop end if '断开数据库 set rs=nothing conn.close set conn=nothing %>
另外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将选择所有表中所有项。
采用枚举法的源程序如下: <%@ CODEPAGE = "936" %> '连接数据库 <% dim conn dim DBOath dim rs dim sql Set conn=Server.CreateObject("ADODB.Connection") DBPath = Server.MapPath("addressbook.mdb") conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath Set rs=Server.CreateObject("ADODB.Recordset") '从Web页获取姓名、电话、学校的值 dim Name dim Tel dim School Name=request("Name") Tel=request("Tel") School=request("School") '枚举法的搜索核心,因为有3个条件所以要写8组If判断语句 if trim(Name)="" and trim(Tel)="" and trim(School)="" then sql="select * from address order by ID asc" end if if trim(Name)="" and trim(Tel)="" and trim(School)<>"" then sql="select * from address where School like '%"&trim(School)&"%' order by ID asc" end if if trim(Name)="" and trim(Tel)<>"" and trim(School)="" then sql="select * from address where Tel like '%"&trim(Tel)&"%' order by ID asc" end if if trim(Name)="" and trim(Tel)<>"" and trim(School)<>"" then sql="select * from address where Tel like '%"&trim(Tel)&"%' and School like '%"&trim(School)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)="" and trim(School)="" then sql="select * from address where Name like '%"&trim(Name)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)="" and trim(School)<>"" then sql="select * from address where Name like '%"&trim(Name)&"%' and School like '%"&trim(School)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)<>"" and trim(School)="" then sql="select * from address where Name like '%"&trim(Name)&"%' and Tel like '%"&trim(Tel)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)<>"" and trim(School)<>"" then sql="select * from address where Name like '%"&trim(Name)&"%' and Tel like '%"&trim(Tel)&"%' and School like '%"&trim(School)&"%' order by ID asc" end if rs.open sql,conn,1,1 '显示搜索结果 if rs.eof and rs.bof then response.write "目前通讯录中没有记录" else do while not rs.eof response.write "姓名:"&rs("Name")&"电话:"&rs("Tel")&"学校:"&rs("School")&"<br>" rs.movenext loop end if '断开数据库 set rs=nothing conn.close set conn=nothing %>
另外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将选择所有表中所有项。