ASP Database Fundamentals (Part 2) - How do I Get At The Data?
(Page 7 of 9 )
We are getting closer to the end of the trail. We can now open recordsets and know if we have data in them. But how do we get at it?
The data in the fields is available by asking for it by name. For example, here is a piece of code to read the first and last names from the recordset:
set rs
= Server.CreateObject("ADODB.recordset") rs.open “select * from Customers”, conn response.write “The customer’s name is “ & rs(“FirstName”) & _ “ “ & rs(“LastName”) rs.close set rs = nothing
You can also access the data by utilizing the “fields” collection of the recordset object. This collection contains all of the columns that are in the recordset. Consider this example:
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 rs.close set rs = nothing
This example would go through all of the columns in the recordset and display the fieldname and the value.
Next: What If There Is More Than One Row? >>
More ASP Articles
More By Rich Smith