通过ASP与ACCESS数据库建立连接(附源码)(3)

(三)显示/查看数据库记录:

1   
2set dbconnection=Server.CREATEOBJECT("ADODB.CONNECTION")   
3DBPath = Server.MapPath("customer.mdb")   
4dbconnection.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath   
5  
6建立与数据库的连接,与上同。   
7  
8SQL="select * from 客户 Order By 时间 DESC"   
9SET CustomerList=dbconnection.EXECUTE(SQL)   

建立SQL查询语句,表示从"客户"表中查询所有记录,Order By 时间 DESC表示"按时间降序排序,"这样最后添加的信息再最上面显示。下面一句执行SQL查询并将结果返回给Recordset对象CustomerList。

 1<html>
 2<body>
 3<font size="5"><center>数据库记录</center></font>
 4<center>
 5<table border="1">
 6<tr>
 7<td>编号</td>
 8<td>公司名称</td>
 9<td>联络人姓名</td>
10<td>所在城市</td>
11<td>电话号码</td>
12</tr>   

DO WHILE NOT CustomerList.EOF

 1  
 2检测记录是否到了最后一条。EOF表示End of File。   
 3  
 4<tr>
 5<td>```
 6 =CustomerList("客户编号") 
 7```</td>
 8<td>```
 9 =CustomerList("公司名称") 
10```</td>
11<td>```
12 =CustomerList("联络人姓名") 
13```</td>
14<td>```
15 =CustomerList("所在城市") 
16```</td>
17<td>```
18 =CustomerList("电话号码") 
19```</td>
20</tr>   
21  

=CustomerList("客户编号")

1  

CustomerList.movenext
loop

 1  
 2如果还没有到最后一条则指针移动到下一条。用Do While ... Loop循环逐一取得所有的记录。   
 3  
 4</table>
 5</center>
 6<center>
 7<input onclick="javascript:location.href='add.htm'" type="button" value="添加数据"/>
 8</center>
 9</body>
10</html>

缺陷:仅仅实现了最基本的功能。先不说其它功能,仅就数据的添加和查看功能来看,比较完美的还应该加入“分页功能”。否则数据大到一定程度显示全部数据基本上是不可能的。
本期至此全部完毕。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus