asp中记录集对象的getrows和getstring用法分析

2019-04-01 14:46:20王冬梅

<TABLE Border=1>
<TR><TD>
<% = Response.Write rs.GetString( , , "</TD><TD>", "</TD></TR><TR>", ) %>
</TABLE>

这样写的HTML结果如下:

<TABLE Border=1>
<TR>
<TD>row1, field1 value</TD>
<TD>row1, field2 value</TD>
</TR>
<TR>
<TD>row2, field1 value</TD>
<TD>row2, field2 value</TD>
</TR>
</TABLE>

这里有个BUG了,再看看生成下拉选单:

<%
Set RS = conn.Execute("Select theValue,theText FROM selectOptionsTable orDER BY theText")
optSuffix = "</OPTION>" & vbNewLine
valPrefix = "<OPTION Value='"
valSuffix = "'>"
opts = RS.GetString( , , valSuffix, optSuffix & valPrefix, "--error--" )
' Next line is the key to it!
opts = Left( opts, Len(opts)-Len(valPrefix) )

Response.Write "<Select ...>" & vbNewLine
Response.Write valPrefix & opts
Response.Write "</Select>"
%>

如果想建立一个正确的表格的话,解决那个BUG,只要这样做就可以了:

<%
Set RS = conn.Execute("Select * FROM table")
tdSuffix = "</TD>" & vbNewLine & "<TD>
trPrefix = "<TR>" & vbNewLine & "<TD>"
trSuffix = "</TD>" & vbNewLine & "</TR>" & vbNewLine & "<TR>" & vbNewLine
opts = RS.GetString( , , tdSuffix, trSuffix & trPrefix, "--error--" )
' Next line is the key to it!
opts = Left( opts, Len(opts)-Len(trPrefix) )
Response.Write "<TABLE Border=1 CellPadding=5>" & vbNewLine
Response.Write trPrefix & opts
Response.Write "</TABLE>" & vbNewLine
%>

再介绍一个完全不同的办法:

<%
SQL = "Select '<OPTION Value=''',value,'''>',text,'</OPTION>' FROM table orDER BY text"
Set RS = conn.Execute(SQL)
Response.Write "<Select>" & vbNewLine & RS.GetString(,,"",vbNewLine) & "</Select>"
%>

你用过吗。。。

看到了吗?可以直接从查询中返回结果。
再进一步,您可以这样做:

<%
SQL = "Select '<OPTION Value=''' & value & '''>' & text & '</OPTION>' FROM table orDER BY text"
Set RS = conn.Execute(SQL)
Response.Write "<Select>" & vbNewLine & RS.GetString(,,"",vbNewLine) & "</Select>"
%>

下面是一份完整的示例:
Script Output:
711855 Wednesday 23 3/23/2005 1:33:37 AM
711856 Wednesday 23 3/23/2005 1:23:00 AM
711857 Wednesday 23 3/23/2005 1:26:34 AM
711858 Wednesday 23 3/23/2005 1:33:53 AM
711859 Wednesday 23 3/23/2005 1:30:36 AM

ASP完整代码如下:

<%
' Selected constants from adovbs.inc:
Const adClipString = 2