ADO 显示

显示来自记录集中的数据的最常用的方法,就是把数据显示在 HTML 表格中。

显示记录

如何首先创建一个数据库连接,然后创建一个记录集,然后把其中的数据显示在HTML中。

  1. <html>
  2. <body>
  3. <%
  4. set conn=Server.CreateObject("ADODB.Connection")
  5. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  6. conn.Open(Server.Mappath("/db/northwind.mdb"))
  7. set rs = Server.CreateObject("ADODB.recordset")
  8. rs.Open "Select * from Customers", conn
  9. do until rs.EOF
  10. for each x in rs.Fields
  11. Response.Write(x.name)
  12. Response.Write(" = ")
  13. Response.Write(x.value & "<br />")
  14. next
  15. Response.Write("<br />")
  16. rs.MoveNext
  17. loop
  18. rs.close
  19. conn.close
  20. %>
  21. </body>
  22. </html>

在 HTML 表格中显示记录

如何把数据表中的数据显示在HTML表格中。

  1. <html>
  2. <body>
  3. <%
  4. set conn=Server.CreateObject("ADODB.Connection")
  5. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  6. conn.Open(Server.Mappath("/db/northwind.mdb"))
  7. set rs = Server.CreateObject("ADODB.recordset")
  8. rs.Open "SELECT Companyname, Contactname FROM Customers", conn
  9. %>
  10. <table border="1" width="100%">
  11. <%do until rs.EOF%>
  12. <tr>
  13. <%for each x in rs.Fields%>
  14. <td><%Response.Write(x.value)%></td>
  15. <%next
  16. rs.MoveNext%>
  17. </tr>
  18. <%loop
  19. rs.close
  20. conn.close
  21. %>
  22. </table>
  23. </body>
  24. </html>

向 HTML 表格添加标题

如何向HTML表格添加标题,以使其可读性更强。

  1. <html>
  2. <body>
  3. <%
  4. set conn=Server.CreateObject("ADODB.Connection")
  5. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  6. conn.Open(Server.Mappath("/db/northwind.mdb"))
  7. set rs = Server.CreateObject("ADODB.recordset")
  8. sql="SELECT Companyname, Contactname FROM Customers"
  9. rs.Open sql, conn
  10. %>
  11. <table border="1" width="100%">
  12. <tr>
  13. <%for each x in rs.Fields
  14. response.write("<th>" & x.name & "</th>")
  15. next%>
  16. </tr>
  17. <%do until rs.EOF%>
  18. <tr>
  19. <%for each x in rs.Fields%>
  20. <td><%Response.Write(x.value)%></td>
  21. <%next
  22. rs.MoveNext%>
  23. </tr>
  24. <%loop
  25. rs.close
  26. conn.close
  27. %>
  28. </table>
  29. </body>
  30. </html>

向 HTML 表格添加颜色

如何向HTML表格添加颜色,以使其更加美观。

  1. <html>
  2. <body>
  3. <%
  4. set conn=Server.CreateObject("ADODB.Connection")
  5. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  6. conn.Open(Server.Mappath("/db/northwind.mdb"))
  7. set rs = Server.CreateObject("ADODB.recordset")
  8. sql="SELECT Companyname, Contactname FROM Customers"
  9. rs.Open sql, conn
  10. %>
  11. <table border="1" width="100%" bgcolor="#fff5ee">
  12. <tr>
  13. <%for each x in rs.Fields
  14. response.write("<th align='left' bgcolor='#b0c4de'>" & x.name & "</th>")
  15. next%>
  16. </tr>
  17. <%do until rs.EOF%>
  18. <tr>
  19. <%for each x in rs.Fields%>
  20. <td><%Response.Write(x.value)%></td>
  21. <%next
  22. rs.MoveNext%>
  23. </tr>
  24. <%loop
  25. rs.close
  26. conn.close
  27. %>
  28. </table>
  29. </body>
  30. </html>

显示字段名称和字段值

我们有一个名为 "Northwind" 的数据库,并且我们希望显示出 "Customers" 表中的数据(记得以 .asp 为扩展名来保存这个文件):

  1. <html>
  2. <body>
  3.  
  4. <%
  5. set conn=Server.CreateObject("ADODB.Connection")
  6. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  7. conn.Open "c:/webdata/northwind.mdb"
  8.  
  9. set rs = Server.CreateObject("ADODB.recordset")
  10. rs.Open "SELECT * FROM Customers", conn
  11.  
  12. do until rs.EOF
  13. for each x in rs.Fields
  14. Response.Write(x.name)
  15. Response.Write(" = ")
  16. Response.Write(x.value & "<br />")
  17. next
  18. Response.Write("<br />")
  19. rs.MoveNext
  20. loop
  21.  
  22. rs.close
  23. conn.close
  24. %>
  25.  
  26. </body>
  27. </html>

在一个 HTML 表格中显示字段名称和字段的值

我们也可以通过下面的代码把表 "Customers" 中的数据显示在一个 HTML 表格中:

  1. <html>
  2. <body>
  3.  
  4. <%
  5. set conn=Server.CreateObject("ADODB.Connection")
  6. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  7. conn.Open "c:/webdata/northwind.mdb"
  8.  
  9. set rs = Server.CreateObject("ADODB.recordset")
  10. rs.Open "SELECT Companyname, Contactname FROM Customers", conn
  11. %>
  12.  
  13. <table border="1" width="100%">
  14. <%do until rs.EOF%>
  15. <tr>
  16. <%for each x in rs.Fields%>
  17. <td><%Response.Write(x.value)%></td>
  18. <%next
  19. rs.MoveNext%>
  20. </tr>
  21. <%loop
  22. rs.close
  23. conn.close
  24. %>
  25. </table>
  26.  
  27. </body>
  28. </html>

向 HTML 表格添加标题

我们希望为这个 HTML 表格添加标题,这样它就更易读了:

  1. <html>
  2. <body>
  3.  
  4. <%
  5. set conn=Server.CreateObject("ADODB.Connection")
  6. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  7. conn.Open "c:/webdata/northwind.mdb"
  8.  
  9. set rs = Server.CreateObject("ADODB.recordset")
  10. sql="SELECT Companyname, Contactname FROM Customers"
  11. rs.Open sql, conn
  12. %>
  13.  
  14. <table border="1" width="100%">
  15. <tr>
  16. <%for each x in rs.Fields
  17. response.write("<th>" & x.name & "</th>")
  18. next%>
  19. </tr>
  20. <%do until rs.EOF%>
  21. <tr>
  22. <%for each x in rs.Fields%>
  23. <td><%Response.Write(x.value)%></td>
  24. <%next
  25. rs.MoveNext%>
  26. </tr>
  27. <%loop
  28. rs.close
  29. conn.close
  30. %>
  31. </table>
  32.  
  33. </body>
  34. </html>