如何统计ASP经典中的记录?

时间:2009-06-29 08:26:57

标签: asp-classic

我不太熟悉ASP经典编程。我只需要在我的网页上运行一个小代码。我如何计算返回查询的记录?

<%
Set rsscroll = Server.CreateObject("ADODB.Recordset")
Dim strSQLscroll, rsscroll
strSQLscroll = "SELECT * FROM tblItems where expiration_date > getdate() order by expiration_date desc;"
rsscroll.open strSQLscroll,oConn
%>

感谢,

9 个答案:

答案 0 :(得分:8)

可以(但不推荐)在Recordset对象上使用RecordCount属性,如下所示:

iTotalRecords = rsscroll.RecordCount

如果你的桌子很大,这可能需要很长时间才能运行。我会改为运行一个单独的SQL查询来获取总记录

SQL = "SELECT COUNT(*) AS TotalRecords FROM tblItems WHERE expiration_date > getdate() "
set rsRecordCount = conn.Execute(SQL)
if not rsRecordCount.Eof then
  iTotalRecords = rsRecordCount.Fields("TotalRecords")
else
  iTotalRecords = 0
end if
rsRecordCount.Close
set rsRecordCount = nothing

答案 1 :(得分:6)

rsscroll。RecordCount

答案 2 :(得分:2)

使用SQL COUNT方法的一个简单解决方案。这假设您需要行数而不是数据本身。

<%
    Set rsscroll = Server.CreateObject("ADODB.Recordset")
    Dim strSQLscroll, rsscroll, intRow
    strSQLscroll = "SELECT COUNT(*) AS Total FROM tblItems WHERE expiration_date > getdate();"
    rsscroll.open strSQLscroll, oConn
    response.write rsscroll("Total")
    rsscroll.close: set rsscroll = nothing 
    oConn.close: set oConn = nothing 
%>

这将返回一行,其中包含一个名为“Total”的值。 (如果您需要行计数数据,请继续阅读。)

您的查询代码使用默认的RecordSet,它以“仅向前”模式返回数据以提高效率。它将逐行逐行,但不知道实际的计数。 (此模式还将RecordSet.RecordCount设置为-1,因此该字段对您无用。)

RecordSet.Open的“Cursor Type”参数允许您更改为“Keyset”模式(参数值1), 将RecordCount字段设置为数据行数。 (为了完整性,包括“锁定类型”和“命令类型”参数,但它们没有考虑到这个答案。)

RecordsetObject.Open "TableName|SQLStatement", ConnectionObject [,Cursor Type] [,Lock Type] [,Command Type] 

将此参数添加到代码的RecordSet.Open调用中,然后检查RecordCount。

<%
    Set rsscroll = Server.CreateObject("ADODB.Recordset")
    Dim strSQLscroll, rsscroll, intRow
    strSQLscroll = "SELECT * FROM tblItems where expiration_date > getdate() order by expiration_date desc;"
    rsscroll.open strSQLscroll, oConn, 1
    intRow = rsscroll.RecordCount
    ' ... do something with intRow
    rsscroll.close: set rsscroll = nothing 
    oConn.close: set oConn = nothing 
%>

如果数据库性能对您的情况有意义,那么 RecordSet.GetRows()方法会更有效。

<% 
    Dim rsscroll, intRow, rsArray
    Set oConn = CreateObject("ADODB.Connection") 
    oConn.open "<connection string>" 
    strSQLscroll = "SELECT * FROM tblItems where expiration_date > getdate() order by expiration_date desc" 
    Set rsscroll = conn.execute(strSQLscroll) 
    if not rsscroll.eof then 
        rsArray = rsscroll.GetRows() 
        intRow = UBound(rsArray, 2) + 1 
        response.write "rows returned: " & intRow
        ' ... do any other operations here ... 
    end if 
    rsscroll.close: set rsscroll = nothing 
    oConn.close: set oConn = nothing 
%>

答案 3 :(得分:1)

您可以更改SQL以计算记录:

strSQLscroll = "SELECT count(*) as Total FROM tblItems where expiration_date > getdate();"

然后你需要response.write rsscroll("Total")

答案 4 :(得分:1)

我通常使用单独的查询,例如“从表中选择计数(*)”来获取计数,因为我通常不仅需要计数,而且还需要单位数或平均价格或其他的总和,并且更容易编写单独查询比制作更多变量并说“TotalUnits = TotalUnits + rs(”Units“)。value”在循环内部显示结果。它还可以在您需要显示结果上方的总数时使用,并且您不希望循环记录集两次。

答案 5 :(得分:1)

进入存储数组中返回数据的习惯。迭代比使用开放记录集要快得多。此外,指定要在执行此操作时选择的字段,因为您必须显式引用数组索引。

<%
Set rsscroll = Server.CreateObject("ADODB.Recordset")
Dim strSQLscroll, rsscroll
Dim arrCommon

'Open recordset, copy data to array
strSQLscroll = "SELECT field1, field2, field3 FROM tblItems where expiration_date > getdate() order by expiration_date desc;"
rsscroll.open strSQLscroll,oConn
    arrCommon = rsscroll.getRows()
rsscroll.close

'Get the total records in this array
response.write ubound(arrCommon, 2);

'Loop...
for i = 0 to ubound(arrCommon, 2)

    ' This prints field 3
    response.write arrCommon(2, i)

next
%>

答案 6 :(得分:0)

&LT;% 'TableID =您的表ID ...

Set rsscroll = Server.CreateObject("ADODB.Recordset") Dim strSQLscroll, rsscroll strSQLscroll = "SELECT *,(SELECT TableID FROM tblItems where expiration_date > getdate()) As Count FROM tblItems where expiration_date > getdate() order by expiration_date desc;" 
rsscroll.open strSQLscroll,oConn
Count = rsscroll("Count") 

%GT;

答案 7 :(得分:0)

你可以试试这个

    Dim count
    count = 0
    if strSQLscroll.eof <> true or strSQLscroll.bof <> true then
       while not strSQLscroll.eof
          count = count+1
          strSQLscroll.movenext
       wend
    end if
    response.write(count)

答案 8 :(得分:0)

如果您使用的是MySQL,请尝试:

Dim strSQLscroll, rsscroll, countrs

Set rsscroll = Server.CreateObject("ADODB.Recordset")
rsscroll.CursorLocation = 3
rsscroll.open "SELECT * FROM tblItems where expiration_date > getdate()
order by expiration_date desc;",oConn

countrs = rsscroll.recordcount