<!-- 多条件组合查询在很多地方都很有用,本文用一个简单的例子来实现一种组合查询 在示例之前请确保你安装有sqlserver2000及其默认数据库NorhtWind. 代码非常直观,加上关键部分我做了注释,所以很容易理解. 需要注意的几个问题就是: 1.在字符串连接的时候注意两个需要连接的串中第二个串的开头第一个字符应该打一个空格,这样不至于两个串的首尾相连成一个单词. 2.righ的left函数取出的结构区分大小写,如果你字符串里用的是'and',而你判断的时候用的是'AND',程序就不会按你所想的运行了 3.注意JS中,history.go(-1),history.back(),location.reload,location.href的区别和实际应用 提醒大家平时多做一些数据结构和算法的训练,写程序基础很重要,不要小看一些字符串处理函数和正则表达式等的应用,好多大的项目正是由一些小函数组合在一起形成的. --> <% If Trim(Request.Form(submit))<> Then Dim arr_wawa,rs_wawa,strconn,sql Set rs_wawa=Server.CreateObject(ADODB.RecordSet) strconn=Driver={sql server};server=192.168.0.110;database=northwind;uid=sa;pwd=sa; '获取表单变量,这里只过滤了空格,但在实际应用中为了安全,需要过滤更多的危险字符. EmployeeID=Trim(Request.Form(EmployeeID)) TitleOfcourtesy=Trim(Request.Form(TitleOfcourtesy)) FirstName=Trim(Request.Form(FirstName)) LastName=Trim(Request.Form(LastName)) Title=Trim(Request.Form(Title)) City=Trim(Request.Form(City)) '以下是动态生成一个sql查询字符串 '************************************* sql=SELECT EmployeeID,TitleOfCourtesy,FirstName,LastName,Title,City FROM Employees WHERE If EmployeeID<> Then sql=sql& EmployeeID=&EmployeeID& AND If TitleOfCourtesy<> Then sql=sql& TitleOfcourtesy='&TitleOfCourtesy&' AND If FirstName<> Then sql=sql& FirstName like '%&FirstName&%' AND If LastName<> Then sql=sql& LastName like '%&LastName&%' AND If Title<> Then sql=sql& Title like '%&Title&%' AND If City<> Then sql=sql& City like '%&City&%' 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 sql=sql& ORDER BY EmployeeID DESC 'Response.Write sql '调试语句 '************************************** rs_wawa.Open sql,strconn,1,3 If Not(rs_wawa.Eof And Rs_wawa.Bof) Then arr_wawa=rs_wawa.getrows() '取出离线的记录集,取出记录集随后就可以关闭记录集,可以节省资源 Else Response.Write(<script>alert('没有记录');history.go(-1);</script>) End If rs_wawa.close() set rs_wawa=nothing End if %> <form name=form1 method=post action=> <table width=500 border=1 align=center cellspacing=1> <tr> <td colspan=2 align=right><div align=center>Search Employees</div></td> </tr> <tr> <td width=50% align=right>EmployeeID:</td> <td width=50%><input name=EmployeeID type=text id=EmployeeID></td> </tr> <tr> <td width=50% align=right>TitleOfCourtesy:</td> <td width=50%><select name=TitleOfCourtesy id=TitleOfCourtesy> <option value=>==select==</option> <option value=Ms.>Ms.</option> <option value=Dr.>Dr.</option> <option value=Mrs.>Mrs.</option> </select></td> </tr> <tr> <td width=50% align=right>FirstName:</td> <td width=50%><input name=FirstName type=text id=FirstName></td> </tr> <tr> <td width=50% align=right>LastName:</td> <td width=50%><input name=LastName type=text id=LastName></td> </tr> <tr> <td width=50% align=right>Title:</td> <td width=50%><input name=Title type=text id=Title></td> </tr> <tr> <td width=50% align=right>City:</td> <td width=50%><input name=City type=text id=City></td> </tr> <tr> <td width=50% align=right> <input type=submit name=Submit value=提交></td> <td width=50%> <input type=reset name=Submit2 value=重置></td> </tr> </table> </form> <% If Trim(Request.Form(submit))<> Then Response.Write(<table width='100%' border='0' cellspacing='1'>) Dim i,j,Rows,Flds Rows=Ubound(arr_wawa,2) Flds=Ubound(arr_wawa,1) If Rows>=0 Then For i=0 To Rows Response.Write(<tr>) For j=0 to Flds Response.Write<td>&arr_wawa(j,i)&</td> Next Response.Write(</tr>) Next Else Response.Write(<tr><td>&Rows&</td></tr>) End If Response.Write(</table>) End If %>
|