填充数据库中的下拉列表

(注意事项:如果遇到使用记录集而不是命令和存储过程的代码,有许多程序员会进入绝对的 conniptions。)

<%
dim rs, sql
dim SelectedUser
SelectedUser = request.form("user")
if IsNumeric(SelectedUser) then
    SelectedUser = CLng(SelectedUser)
else
    SelectedUser = 0
end if
%>
...
<p>Select a user: <select name="user" size="1">
<%
sql = "SELECT id, displayname FROM users WHERE active = 1 ORDER BY displayname"
set rs = server.createobject("ADODB.Recordset")
rs.open sql,"[connection string stuff goes here]",1,2
do until rs.eof
    response.write "<option value='" & rs("id") & "'"
    if rs("id") = SelectedUser then response.write " selected"
    response.write ">" & rs("displayname") & "</option>" & vbCrLf
    rs.Movenext '<- VERY VERY IMPORTANT!
loop
rs.close
set rs = nothing
%>
</select></p>
...