Hiển thị ADO

Phương pháp phổ biến nhất để hiển thị dữ liệu từ tập tin ghi là hiển thị dữ liệu trong bảng HTML.

Mô hình

Hiển thị bản ghi
Cách đầu tiên là tạo kết nối cơ sở dữ liệu, sau đó tạo tập tin ghi và hiển thị dữ liệu trong HTML.
Hiển thị bản ghi trong bảng HTML
Cách hiển thị dữ liệu trong bảng HTML.
Thêm tiêu đề cho bảng HTML
Cách thêm tiêu đề vào bảng HTML để làm cho nó dễ đọc hơn.
Thêm màu sắc vào bảng HTML
Cách thêm màu sắc vào bảng HTML để làm cho nó đẹp hơn.

Hiển thị tên trường và giá trị trường

Chúng tôi có cơ sở dữ liệu có tên "Northwind" và chúng tôi muốn hiển thị dữ liệu của bảng "Customers" (nhớ lưu tệp với đuôi .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>

Hiển thị tên trường và giá trị trường trong bảng HTML

Chúng tôi cũng có thể hiển thị dữ liệu của bảng "Customers" trong bảng HTML bằng mã sau:

<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>

Thêm tiêu đề cho bảng HTML

Chúng tôi hy vọng thêm tiêu đề cho bảng HTML này để nó dễ đọc hơn:

<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>