html code:
<form name="frm_Search" method="get" action="Search.asp">
请输入关键字:
<input type="text" name="key" size="10">
<input type="submit" value="搜索">
</form>
asp.net code
<!--搜索开始-->
<%
Dim strProvider,CNN
'strProvider="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
'strProvider=strProvider & Server.MapPath("") & "datadb_Sample.mdb" '假设数据库存放在主页根目录下的data目录下
'Set CNN = Server.CreateObject("ADODB.connection")
'CNN.Open strProvider '打开数据库连接 set CNN = server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB;Server=localhost;uid=sa;pwd=1qaz2wsx;database=zgweb"
CNN.Open constr
'sql="select * from TB_USERINFO"
'set CNN = conn.execute(sql)
%> <%
'接下来,判断 ASP页所接收到的数据,并在数据库中进行搜索。
Dim S_Key,RST,StrSQL
S_Key = Trim(Request("key")) '得到搜索关键字的值
If S_Key <>"" then
Set RST=Server.CreateObject("ADODB.RecordSet")
StrSQL=AutoKey(S_Key) '此处使用自定义函数 AutoKey(),该函数为实现智能搜索的核心
RST.Open StrSQL,CNN,, '得到搜索后的记录 If RST.BOF And RST.EOF Then
%>
<font color="#FF0000">未找到任何结果!!!</font>
<%
Else
%>
搜索名称为“<font color="#FF0000"><%= S_Key %></font>”的项,共找到 <font color="#FF0000"><%= RST.RecordCount %></font> 项:<p>
<%
While Not RST.EOF '遍历整个记录集,显示搜索到的信息并设置链接
%>
<!-- 此处可设为你所需要的链接目标 -->
<font style="font: 12pt 宋体"><a href="info.aspID=<%= RST("source") %>" target="_blank"><%= RST("usersex") %></a></font><br>
<!-- 显示部分详细内容 -->
<font style="font: 9pt 宋体"><%= Left(RST("usercompany"),) %></font><p>
<%
RST.MoveNext
Wend
RST.Close
Set RST=Nothing
End If
End If
%>
<%
Function AutoKey(strKey)
CONST lngSubKey=
Dim lngLenKey, strNew1, strNew2, i, strSubKey '检测字符串的合法性,若不合法则转到出错页。出错页你可以根据需要进行设定。
'if InStr(strKey,"=")<>0 or InStr(strKey,"`")<>0 or InStr(strKey,"'")<>0 or InStr(strKey," ")<>0 or InStr(strKey," ")<>0 or InStr(strKey,"'")<>0 or InStr(strKey,chr(34))<>0 or InStr(strKey,"")<>0 or InStr(strKey,",")<>0 or InStr(strKey,"<")<>0 or InStr(strKey,">")<>0 then
'Response.Redirect "error.htm" 'End If lngLenKey=Len(strKey) Select Case lngLenKey
Case '若为空串,转到出错页
Response.Redirect "error.htm" Case '若长度为1,则不设任何值
strNew1=""
strNew2=""
Case Else '若长度大于1,则从字符串首字符开始,循环取长度为2的子字符串作为查询条件
For i= To lngLenKey-(lngSubKey-)
strSubKey=Mid(strKey,i,lngSubKey)
strNew1=strNew1 & " or usersex like '%" & strSubKey & "%'"
strNew2=strNew2 & " or usercompany like '%" & strSubKey & "%'" Next
End Select '得到完整的SQL语句
AutoKey="Select * from TB_USERINFO where usersex like '%" & strKey & "%' or usercompany like '%" & strKey & "%'" & strNew1 & strNew2
'response.Write AutoKey
' response.end() End Function
%>
<%
cnn.Close
Set cnn=Nothing
%> <!--搜索结束-->
effect:
分页方法
'========================
'getNewsList
'新闻列表+分页
' 调用GotTopic,获取数据
'=========================
Function getNewsListPage(sql,titleNumber,widthNumber,page)
'set rs = conn.execute(sql)
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,, If rs.eof Then
Else
rs.pagesize=
page=request.querystring("page")
If Not Isnumeric(page) or page="" Then
page=
else
page=cint(page)
End if
if page< then page=
if page>rs.pagecount then page=rs.pagecount
rs.AbsolutePage = page
for i= to rs.pagesize response.Write "<li class=""""><a href=""shownews.asp?news_id="& rs("news_id") &""" title="&rs("news_title") &"target=""_blank""><span class=""yuandian"">●</span> "& gotTopic(rs("news_title"),titleNumber)&"</a><label style=""text-align:right;"">"& jdate(rs("news_date")) &"</label></li>" rs.movenext
if rs.eof then
Exit For
End if
next
end if response.Write "<div class=""pageStyle"">"
response.Write "<ul class=""kkpage"">"
if rs.pagecount> then '--判断页码是否大于1,大于不显示分页,反之显示分页 if page> then response.Write "<li id=""starpage""><a Href=""aload.asp?cat_id="&request("cat_id")&"&Page = 1"">首页</a> </li> "
response.Write "<li id='PreviousPage'><a Href=""aload.asp?cat_id="&request("cat_id")&"&Page=page -1 "">上一页</a></li>" else response.Write " <li id=""currentpage"" style=""background:#dedede;""><a>首页</a></li> " response.Write "<li style=""background:#dedede;"" id='currentpage'><a>上一页</a></li>" end if if page> then StartPage=page- else StartPage= if page<rs.pagecount- then EndPage=page+ else EndPage=rs.pagecount 'end if if page<rs.pagecount- then EndPage=page+ else EndPage=rs.pagecount for j=StartPage to EndPage
if j=page then
response.write "<li id='AbsPage'><a href=aload.asp?cat_id="&request("cat_id")&"&page="&j&">"&j&"</a></li>"
else
response.write "<li><a href=aload.asp?cat_id="&request("cat_id")&"&page="&j&">"&j&"</a></li>"
end if
next
if page<>rs.pagecount then
response.write "<li id='NextPage'><a Href='aload.asp?cat_id="&request("cat_id")&"&Page="&(page+)&"'>下一页</a> <span></span></li>" response.write "<li id='endpage'><a Href=""aload.asp?cat_id="&request("cat_id")&"&Page= rs.movenext.PageCount"">尾页</a> </li>" else response.write "<li style=""background:#dedede;"" id='currentpage'><a>下一页</a></li>"
response.write "<li id=""currentpage"" style=""background:#dedede;""><a>尾页</a></li>"
end if end if response.write "</ul>"
response.write "</div>"
'set rs.movenext=nothing 'rs.movenext End function