假设两个文本框的名字分别为t1和t2.当按下提交按钮后,跳转到另外一个处理页面,这里假设为search.asp吧,数据库为sqlserver2000代码如下:
<%
option explicit
dim t1
dim t2
dim sql
dim rs
dim conn
t1=request.form("t1")
t2=request.form("t2")
set conn=server.createobject("adodb.connection")
conn.connectionstring="driver={sql server};server=(local);UID=[yourloginname];PWD=[yourpassword];database=[yourdatabasename]"
conn.open
set rs=server.createobject("adodb.recordset") '昨晚由于太累了,写错了,应该是"adodb.recordset".现在改回来了.
if t1<>'' and t2='' then
sql="select * from [tablename] where [columnname1] like '"&t1&"'"
else if t1='' and t2<>'' then
sql="select * from [tablename] where [columnname2] like '"&t2&"'"
else if t1<>'' and t2<>'' then
sql="select * from [tablename] where [columnname1]='"&t1&"' and [columnname2]='"&t2&"'"
else
response.write "<script>alert('请填写要查询的条件!');history.back();</script>"
end if
end if
end if
end if
conn.execute(sql)
rs.open sql,conn,1,1
.
.
.
rs.close
conn.close
set rs=nothing
set conn=nothing
%>
当然了,如果写成存储过程的话,效率会高很多.如果你的是ACCESS数据库的话,改相应的连接就可以了....'' and t2='' then
sql="select * from [tablename] where [columnname1] like '"&t1&"'"
else if t1='' and t2<>'' then
sql="select * from [tablename] where [columnname2] like '"&t2&"'"
else if t1<>'' and t2<>'' then
sql="select * from [tablename] where [columnname1]='"&t1&"' and [columnname2]='"&t2&"'"
else
response.write "alert('请填写要查询的条件!');history.back();"
end if
end if
end if
end if
conn.execute(sql)
rs.open sql,conn,1,1
.
.
.
rs.close
conn.close
set rs=nothing
set conn=nothing
%>