liu_libra 发布留言 2008-10-16 12:41
各位帮我看看我的搜索功能哪里出问题了?
说明:共4个搜索条件,分别为class1,class2,城市和关键字
其中class1 和class2 两者不同时为空,城市(province,city)和关键字(keywords)可以为空
代码如下,请帮我看看那里出问题了,错误提示为ADODB.Recordset (0x800A0BB9)参数类型不正确,或不在可以接受的范围之内,或与其他参数冲突。(连接数据库正确)或者有没有其他的方法可行。我觉得这样太复杂了,谢谢了!
<%
set rs2=server.CreateObject("adodb.recordset")
if request("class1")="" and request("class2")<>"" then
if request("province")="" and request("keywords")="" then
sql="select * from ad where class2='"&request("class2")&"'"
else
if request("province")<>"" and request("keywords")<>"" then
sql="select * from ad where class2='"&request("class2")&"' and province='"&request("province")&"' and city='"&request("city")&"' and keywords like '%"&request("keywords")&"%'"
else
if request("province")<>"" and request("keywords")="" then
sql="select * from ad where class2='"&request("class2")&"' and province='"&request("province")&"' and city='"&request("city")&"'"
else
if request("province")="" and request("keywords")<>"" then
sql="select * from ad where class2='"&request("class2")&"' and keywords like '%"&request("keywords")&"%'"
end if
end if
end if
end if
else
if request("class1")<>"" and request("class2")="" then
if request("province")="" and request("keywords")="" then
sql="select * from ad where class1='"&request("class1")&"'"
else
if request("province")<>"" and request("keywords")<>"" then
sql="select * from ad where class1='"&request("class1")&"' and province='"&request("province")&"' and city='"&request("city")&"' and keywords like '%"&request("keywords")&"%'"
else
if request("province")<>"" and request("keywords")="" then
sql="select * from ad where class1='"&request("class1")&"' and province='"&request("province")&"' and city='"&request("city")&"'"
else
if request("province")="" and request("keywords")<>"" then
sql="select * from ad where class1='"&request("class1")&"' and keywords like '%"&request("keywords")&"%'"
end if
end if
end if
end if
else
if request("class1")<>"" and request("class2")<>"" then
if request("province")="" and request("keywords")="" then
sql="select * from ad where class2='"&request("class2")&"' and class1='"&request("class1")&"'"
else
if request("province")<>"" and request("keywords")<>"" then
sql="select * from ad where class2='"&request("class2")&"' and class1='"&request("class1")&"' and province='"&request("province")&"' and city='"&request("city")&"' and keywords like '%"&request("keywords")&"%'"
else
if request("province")<>"" and request("keywords")="" then
sql="select * from ad where class2='"&request("class2")&"' and class1='"&request("class1")&"' and province='"&request("province")&"' and city='"&request("city")&"'"
else
if request("province")="" and request("keywords")<>"" then
sql="select * from ad where class2='"&request("class2")&"' and class1='"&request("class1")&"' and keywords like '%"&request("keywords")&"%'"
end if
end if
end if
end if
end if
end if
end if
rs2.open str,conn,1,1
%>
multiple1902 发布留言 2008-10-16 13:35
我不看这么长的生成SQL语句的代码,你自己检查最后生成的SQL语句有没有问题……遇到问题再想怎么办
hmhz 发布留言 2008-10-16 13:53
完全可以这样写
if …… then
……
elseif …… then
……
end if
孤独冷雨 发布留言 2008-10-16 14:20
先把要搜索的四个关键接收过来.再用IF判断写SQL语句.代码简单明了,你那样太乱了!
liu_libra 发布留言 2008-10-16 15:05
谢谢各位的指点,我再试试看
lili0610 发布留言 2008-10-16 15:28
[free]sql="Select BellID,SongName,SingerName,Price,AvailabilityDate,SpName,Region,DownloadsNumber,DownloadsDate,YearMonth from DownloadsData where"
if music<>"" then sql=sql&" SongName like '%"&music&"%' AND"
if m_name<>"" then sql=sql&" SingerName like '%"&m_name&"%' AND"
if spname<>"" then sql=sql&" SpName like '%"&spname&"%' AND"
if region<>"" then sql=sql&" Region like '%"®ion&"%' AND"
if date1<>"" and date2="" then sql=sql&" YearMonth ='"&date1&"-"&day1&"' AND"
if date2<>"" and date1="" then sql=sql&" YearMonth ='"&date2&"-"&day1&"' AND"
if date1<>"" and date2<>"" then sql=sql&" YearMonth >= '"&date1&"-"&day1&"' AND YearMonth <= '"&date2&"-"&day1&"' AND"
DownloadsDate>'"&month1&"' AND"
if right(sql,5)="WHERE" then sql=left(sql,clng(len(sql))-5) '解决所有参数都为空的情况
if right(sql,3)="AND" then sql=left(sql,clng(len(sql))-3) '去掉参数最后的and[/free]
用这个吧?我看比你的要好看明白的多,也没有你的麻烦
想怎么查询就怎么查询
这只是一种方法
liu_libra 发布留言 2008-10-16 19:06
我试试,谢谢6楼了!
页: [1]