Hiển thị ADO
Cách phổ biến nhất để hiển thị dữ liệu từ tập bản ghi là hiển thị dữ liệu trong bảng HTML.
Hiển thị Tên trường và Giá trị Trường
Chúng tôi có một cơ sở dữ liệu tên là "Northwind" và chúng tôi muốn hiển thị dữ liệu từ bảng "Khách hàng" (hãy nhớ lưu tệp với phần mở rộng .asp):
Thí dụ
<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 từ bảng "Khách hàng" bên trong bảng HTML với các dòng sau (hãy nhớ lưu tệp với phần mở rộng .asp):
Thí dụ
<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 đề vào bảng HTML
Chúng tôi muốn thêm tiêu đề vào bảng HTML để làm cho nó dễ đọc hơn (hãy nhớ lưu tệp với phần mở rộng .asp):
Thí dụ
<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>
Các ví dụ khác
Cách thêm màu vào bảng HTML để nó trông đẹp mắt.