十天学会ASP之第六天

学习目的:学会数据库的基本操作2(查询记录)

在第四天中我们有这样一个程序:

1   
2set conn=server.createobject("adodb.connection")   
3conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("example3.mdb")   
4exec="select * from guestbook"   
5set rs=server.createobject("adodb.recordset")   
6rs.open exec,conn,1,1   

我们查询的是所有的记录,但是我们要修改、删除记录的时候不可能是所有记录,所有我们要学习检索合适的记录。先看一条语句:

a="张三"
b=111
exec="select * from guestbook where name='"+a+"'and tel="+b
where后面加上的是条件,与是and,或是or,我想=,<=,>=,<,>的含义大家都知道吧。这句话的意思就是搜索name是张三的,并且电话是111的记录。还有一点就是如果要搜索一个字段里面是不是包含一个字符串就可以这么写:where instr(name,a)也就是搜索name里面有a(张三)这个字符串的人。

我这里的a,b,是常量,大家可以让a,b是表单提交过来的变量,这样就可以做一个搜索了。

下面大家看看这个代码,理解一下:

 1<form action="example6.asp" method="post" name="form1">   
 2搜索:<br/>   
 3name =   
 4<input name="name" type="text"/>   
 5and tel=   
 6<input name="tel" type="text"/>
 7<br/>
 8<input name="Submit" type="submit" value="提交"/>
 9<input name="Submit2" type="reset" value="重置"/>
10</form>

example6.asp:

1   
2name=request.form("name")   
3tel=request.form("tel")   
4set conn=server.createobject("adodb.connection")   
5conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("example3.mdb")   
6exec="select * from guestbook where name='"+name+"' and tel="+tel   
7set rs=server.createobject("adodb.recordset")   
8rs.open exec,conn,1,1   
1<html>
2<head>
3<title>无标题文档</title>
4<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
5</head>
6<body bgcolor="#FFFFFF" text="#000000">
7<table border="0" cellpadding="0" cellspacing="0" width="100%">   

do while not rs.eof

 1<td>```
 2=rs("name")
 3```</td>
 4<td>```
 5=rs("tel")
 6```</td>
 7<td>```
 8=rs("message")
 9```</td>
10<td>```
11=rs("time")
12```</td>
13</tr>   

rs.movenext
loop

1</table>
2</body>
3</html>

今天实际上就讲了一个where,大家回去做做试验,把instr()做进去,明天见!

Published At
Categories with 软件教程
comments powered by Disqus