显示来自记录集中的数据的最常用的方法,就是把数据显示在 HTML 表格中。
我们有一个名为 "Northwind" 的数据库,并且我们希望显示出 "Customers" 表中的数据(记得以 .asp 为扩展名来保存这个文件):
<html> <body> <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" set rs = Server.CreateObject("ADODB.recordset") rs.Open "SELECT * FROM Customers", conn do until rs.EOF for each x in rs.Fields Response.Write(x.name) Response.Write(" = ") Response.Write(x.value & "<br />") next Response.Write("<br />") rs.MoveNext loop rs.close conn.close %> </body> </html>
我们也可以通过下面的代码把表 "Customers" 中的数据显示在一个 HTML 表格中:
<html> <body> <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" set rs = Server.CreateObject("ADODB.recordset") rs.Open "SELECT Companyname, Contactname FROM Customers", conn %><table border="1" width="100%">
<%do until rs.EOF%><tr>
<%for each x in rs.Fields%><td><%Response.Write(x.value)%></td>
<%next rs.MoveNext%></tr>
<%loop rs.close conn.close %></table>
</body> </html>
我们希望为这个 HTML 表格添加标题,这样它就更易读了:
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT Companyname, Contactname FROM Customers"
rs.Open sql, conn
%>
<table border="1" width="100%">
<tr>
<%for each x in rs.Fields
response.write("<th>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
<tr>
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%></td>
<%next
rs.MoveNext%>
</tr>
<%loop
rs.close
conn.close
%>
</table>
</body>
</html>