ADO 显示
显示来自记录集中的数据的最常用的方法,就是把数据显示在 HTML 表格中。
显示记录
如何首先创建一个数据库连接,然后创建一个记录集,然后把其中的数据显示在HTML中。
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/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>
在 HTML 表格中显示记录
如何把数据表中的数据显示在HTML表格中。
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/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表格添加标题,以使其可读性更强。
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/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>
向 HTML 表格添加颜色
如何向HTML表格添加颜色,以使其更加美观。
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT Companyname, Contactname FROM Customers"
rs.Open sql, conn
%>
<table border="1" width="100%" bgcolor="#fff5ee">
<tr>
<%for each x in rs.Fields
response.write("<th align='left' bgcolor='#b0c4de'>" & 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>
显示字段名称和字段值
我们有一个名为 "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>
在一个 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 表格添加标题,这样它就更易读了:
- <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>