问题
I wanted to echo some data from a table in my database and found this code:
set rs = Server.CreateObject("ADODB.recordset")
rs.Open "Select name From users", conn
do until rs.EOF
for each x in rs.Fields
Response.Write(x.value)
next
rs.MoveNext
loop
rs.close
I tested it and and it worked but I have no clue what all that syntax means and there is no explanation provided with the code. Can someone with experience help me out?
回答1:
rs is a recordset, which means a result of a database query.
The do until ... loop iterates (with the help of movenext) through all the rows found in the recordset (i.e. table users).
On all rows found the for each ... next loops through all fields found in the single row, which in this case is only the column name.
回答2:
See comments added to the code below
' Create a recordset. This is an object that can hold the results of a query.
set rs = Server.CreateObject("ADODB.recordset")
' Open the recordset for the query specified, using the connection "conn"
rs.Open "Select name From users", conn
' Loop over the results of the query until End Of File (EOF)
' i.e. move one at a time through the records until there are no more left
do until rs.EOF
' For each field in this record
for each x in rs.Fields
' Write out its value to screen
Response.Write(x.value)
' Move to the next field
next
' Move to the next record
rs.MoveNext
' Continue to loop until EOF
loop
' Close the recordset
rs.close
来源:https://stackoverflow.com/questions/21705266/can-somebody-explain-how-this-asp-code-works