查询一条记录,按所选择的字段输出。
发表于:2007-06-30来源:作者:点击数:
标签:
有以下表单,想查询一条记录,按所选择的字段输出(字段名fieldname1,fieldname2...请按你表中的字段名指定): input.htm form method=Post action=test.asp name=form1 input type=text name=bianhao size=10 input type=checkbox name=c1 value=fieldname1
有以下表单,想查询一条记录,按所选择的字段输出(字段名fieldname1,fieldname2...请按你表中的字段名指定):
input.htm
<form method="Post" action="test.asp" name="form1">
<input type="text" name="bianhao" size="10">
<input type="checkbox" name="c1" value="fieldname1">
<input type="checkbox" name="c1" value="fieldname2">
...
<input type="submit" value="提交">
</form>
对应的ASP文件如下,这个代码的通用性很强(
数据库mymdb.mdb你自己指定,表名tablename你自己指定):
test.asp
<%
Dim fieldnames,fields
fieldnames = ""
‘’遍历表单checkbox,将选择的字段存入字符串变量filednames
For Each fieldname In Request("c1")
fieldnames = fieldnames & "," & fieldname
Next
‘’判断是否有选项
If fieldnames = "" Then
Response.Write "<center><h2>你至少应选择一个查询字段!</h2></center>"
Response.End
End If
‘’连接数据库,打开记录集Rs
StrSql = "Select "& Mid(fieldnames,2) & " From tablename Where bianhao=‘’" & Request("bianhao") & "‘’"
StrConn = "DRIVER={Microsoft A
clearcase/" target="_blank" >ccess Driver (*.mdb)};DBQ=" & Server.MapPath("mymdb.mdb")
Set Rs = Server.CreateObject("ADODB.RecordSet")
Rs.Open StrSql,StrConn,1,1
‘’分解字符串变量filednames,按选择的字段输出结果
fields = Split(fieldnames,",")
For i = 1 To UBound(fields)
Response.Write fields(i) & ":" & Rs(fields(i)) & "<br>"
Next
Rs.Close
%>
原文转自:http://www.ltesting.net