Recordset (bản ghi tập) ADO

Nếu cần đọc dữ liệu từ cơ sở dữ liệu, thì dữ liệu đó phải được tải vào một tập hợp dữ liệu trước.

Tạo một tập hợp dữ liệu bảng ADO (ADO Table Recordset)

Sau khi tạo kết nối cơ sở dữ liệu ADO, như đã được mô tả trong chương trước, tiếp theo có thể tạo một tập hợp dữ liệu ADO.

Giả sử chúng ta có cơ sở dữ liệu có tên là "Northwind", chúng ta có thể truy cập vào bảng "Customers" trong cơ sở dữ liệu thông qua mã sau:

<%
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 "Customers", conn
%>

Tạo một tập hợp dữ liệu ADO SQL (ADO SQL Recordset)

Chúng ta cũng có thể sử dụng SQL để truy cập dữ liệu trong bảng "Customers":

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

Trích xuất dữ liệu từ tập hợp dữ liệu

Sau khi tập hợp dữ liệu được mở, chúng ta có thể trích xuất dữ liệu từ tập hợp dữ liệu.

Giả sử chúng ta sử dụng cơ sở dữ liệu có tên là "Northwind", chúng ta có thể truy cập vào bảng "Customers" trong cơ sở dữ liệu thông qua mã sau:

<%
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
for each x in rs.fields
   response.write(x.name)
   response.write(" = ")
   response.write(x.value)
next
%>

Đối tượng Recordset ADO (ADO Recordset Object)

Đối tượng Recordset ADO có thể được sử dụng để chứa tập hợp các bản ghi từ bảng cơ sở dữ liệu.

Xem tất cả các phương pháp và thuộc tính của đối tượng ADO Recordset.