当用ASP与SQL Server数据库打交道时,查询语句是必不可少的。SQL Server数据库本身提供了丰富的查询语句,但是如何在ASP中实现对SQL Server数据库的多条件动态查询呢?笔者在用ASP开发一个基于SQL Server的网站时,较好地解决了这一问题,本文介绍其中的实现方法。 
	  数据库的定义   
	  在SQL Server中定义一个数据库,名称为“comm_server”。在该数据库中定义一个表,表名为“operator”,包含如下表所示字段 (仅以程序中用到的5个字段为例): 
	字段名称 备注 
	Name 姓名Varchar(20),定义为主键 
	Educationallever 学历 Varchar(10) 
	Grade 职称 Varchar(10) 
	State 现在状况 Varchar(10) 
	Time 记录时间 datetime 
	
	输入网页的设计 
	在index_people.htm网页中定义一个form,其中用到的标准用户界面元素如下: 
	姓名:学历: 选择职称: 选择现在状况: 选择 
	TML程序代码如下: 
	〈!--form的方法设置为post,表单提交后由people_seek.asp程序进行处理--〉 
	〈form method=“post” action=“people_seek.asp”〉 
	〈pre〉 
	〈font size=“2”〉姓名:〈/font〉 
	〈input type=“text” name=“txt_name” size=“10”〉 
	〈font size=“2”〉学历: 
	〈select name=“sel_xueli”〉 
	〈option value=“选择”〉选择〈/option〉 
	〈option value=“中专”〉中专〈/option〉 
	〈option value=“大专”〉大专〈/option〉 
	〈option value=“本科”〉本科〈/option〉 
	〈option value=“硕士”〉硕士〈/option〉 
	〈option value=“博士”〉博士〈/option〉 
	〈option value=“博士后”〉博士后〈/option〉 
	〈/select〉 
	职称: 
	〈select name=“sel_zhicheng”〉 
	〈option value=“选择”〉选择〈/option〉 
	〈option value=“助工”〉助工〈/option〉 
	〈option value=“工程师”〉工程师〈/option〉 
	〈option value=“高级工程师”〉高级工程师〈/option〉 
	〈/select〉 
	现在状况: 
	〈select name=“sel_zhuangkuang”〉 
	〈option value=“选择”〉选择〈/option〉 
	〈option value=“在位”〉在位〈/option〉 
	〈option value=“休假”〉休假〈/option〉 
	〈option value=“出差”〉出差〈/option〉 
	〈/select〉 
	〈input type=“submit” name=“btn_seek” value=“搜索”〉 
	〈input type=“reset” name=“btn_cancel” value=“取消”〉 
	〈input type=“submit” name=“btn_browse” value=“浏览”〉 
	〈/font〉 〈/pre〉 
	〈/form〉 
	多条件动态查询的实现 
	people_seek.asp程序代码如下: 
	〈!--定义以下两个函数,通过ADO连接SQL Server数据库--〉 
	〈% 
	Function GetSQLServerConnection( Computer, UserID, Password, Db ) 
	Dim Params, conn 
	Set GetSQLServerConnection = Nothing 
	Params = “Provider=SQLOLEDB.1” 
	Params = Params &“;Data Source=” & Computer 
	Params = Params & “;User ID=” & UserID 
	Params = Params & “;Password=” & Password 
	Params = Params & “;Initial Catalog=” & Db 
	Set conn = Server.CreateObject 
	(“ADODB.Connection”) 
	conn.Open Params 
	Set GetSQLServerConnection = conn 
	End Function 
	Function GetSQLServerStaticRecordset( conn, source ) 
	Dim rs 
	Set rs = Server.CreateObject(“ADODB.Recordset”) 
	rs.Open source, conn, 3, 2 
	Set GetSQLServerStaticRecordset = rs 
	End Function 
	%〉 
	〈HTML〉〈BODY bgcolor=“#FFFFFF”〉 
	以下是对form表单的处理: 
	〈!--如果在form中点击“搜索”按钮--〉 
	〈% 
	if Request(“btn_seek”)〈〉 Empty then 
	〈!--获得查询者输入信息--〉 
	seek_name=Trim(Request(“txt_name”)) 
	seek_xueli=Trim(Request(“sel_xueli”)) 
	seek_zhicheng=Trim(Request(“sel_zhicheng”)) 
	seek_zhuangkuang=Trim(Request 
	(“sel_zhuangkuang”)) 
	〈!--如果查询者什么都没有输入--〉 
	if((seek_name=“”) and (seek_xueli=“选择”) 
	and (seek_zhicheng=“选择”) and 
	(seek_zhuangkuang=“选择”)) then %〉 
	〈center〉〈h2〉〈font color=“#FF0033”〉 
	〈%Response.Write “您没有输入查询条件!” %〉〈BR〉〈BR〉 
	〈%Response.Write “请输入条件后查询!!!” %〉〈BR〉〈BR〉 
	〈/font〉〈/h2〉 
	〈input type=“button” name=“btn_goback”value=“返回” onclick=“javascript:history.go(-1)”〉 
	〈/center〉 
	〈%Response.End%〉 
	〈% end if 
	〈!--定义要查询的SQL语句--〉 
	sql_text=“select * from operator where ” 
	〈!--查看是否输入了人名--〉 
	if seek_name=“” then 
	〈!--如果没有输入人名--〉 
	sql_text=sql_text 
	else 〈!--如果输入了人名--〉 
	sql_name=“name =‘“&seek_name&”’” 
	sql_text=sql_text+sql_name 
	end if 
	〈!--查看是否选择了学历--〉 
	if seek_xueli=“选择” then 
	〈!--如果没有选择学历--〉 
	sql_text=sql_text 
	else 〈!--如果选择了学历--〉 
	if (seek_name〈〉“”) then 
	〈!--在前面输入了要查询的人名--〉 
	sql_xueli=“ and “+”educationallever =‘“&seek_xueli&”’” 
	else 
	sql_xueli=“educationallever =‘“& 
	seek_xueli&”’” 
	end if 
	sql_text=sql_text+sql_xueli 
	end if 
	〈!--查看是否选择了职称--〉 
	if seek_zhicheng=“选择" then 
	〈!--如果没有选择职称--〉 
	sql_text=sql_text 
	else 〈!--如果选择了职称--〉 
	if ((seek_name〈〉“”) or (seek_xueli〈〉“选择”)) then 
	〈!--在前面输入了人名或选择了学历--〉 
	sql_zhicheng=“ and “+”grade =‘“&seek_zhicheng&”’” 
	else 〈!--仅选择了职称--〉 
	sql_zhicheng=“grade =‘“& 
	seek_zhicheng&”’” 
	end if 
	sql_text=sql_text+sql_zhicheng 
	end if 
	〈!--查看是否选择了现在状况--〉 
	if seek_zhuangkuang=“选择” then 
	〈!--如果没有选择现在状况--〉 
	sql_text=sql_text 
	else 〈!--选择了现在状况--〉 
	if ((seek_name〈〉“”) or (seek_xueli〈〉“选择”) or (seek_zhicheng〈〉“选择”)) then 
	〈!--在前面输入了人名或选择了学历或选择了职称--〉 
	sql_zhuangkuang=“ and “+”state =‘“&seek_zhuangkuang&”’” 
	else 〈!--仅选择了现在状况--〉 
	sql_zhuangkuang=“state =‘“& 
	seek_zhuangkuang&”’” 
	end if 
	sql_text=sql_text+sql_zhuangkuang 
	end if 
	〈!--按记录时间倒序显示--〉 
	sql_text=sql_text+“ order by time desc” 
	else 〈!--如果在form中点击“浏览”按钮--〉 
	sql_text=“select * from operator order by time desc” 
	end if 
	Myself = Request.ServerVariables(“PATH_INFO”) 
	〈!--连接SQL Serve数据库,机器名为“comm_server”,数据库名称为“comm_server ”,以“sa”的身份访问,密码为空--〉 
	Set rs = GetSQLServerStaticRecordset( GetSQLServerConnection(“comm_server”,“sa”,“”,“comm_server” ), sql_text) 
	if rs is nothing then 
	Response.Write “连接数据库失败!” 
	Response.End 
	end if 
	〈!--如果在数据库中没有查询到所要的信息--〉 
	if ((rs.BOF=true) and (rs.EOF=true)) then %〉 
	〈center〉〈h2〉 
	〈font color=“#FF0033”〉 
	〈%Response.Write “数据库中没有您要查询的信息!” %〉〈BR〉〈BR〉 
	〈/font〉〈/h2〉 
	〈a href=“index_people.htm”〉 
	〈font size=4〉返回〈/font〉〈/a〉 
	〈/center〉 
	〈% Response.End 
	end if 
	……%〉 
	  上述ASP程序实现了对SQL Server数据库的多条件动态查询,达到了预定的效果。如果实际中需要实现模糊查询或多表联合查询,只需对上述程序稍加修改即可使用。