【问题标题】:Returning more than 1000 rows in classic asp adodb.recordset在经典 asp adodb.recordset 中返回超过 1000 行
【发布时间】:2011-02-16 18:22:18
【问题描述】:

我在 asp classic 中的代码,做一个 mssql 数据库查询:

        rs.pagesize = 1000  ' this should enable paging
        rs.maxrecords = 0   ' 0 = unlimited maxrecords      

        response.write "hello world 1<br>"
        rs.open strSql, conn 

        response.write "hello world 2<br>"

当返回的行数少于 1000 时,我的输出很好。超过 1000 行,我没有得到“hello world 2”。

我认为设置 pagesize 会设置分页,因此无论有多少行都可以返回所有行。如果不设置 pagesize,则不启用分页,限制为 1000 行。但是,我的页面表现得好像 pagesize 根本不起作用。

请指教。

【问题讨论】:

  • 一次对数据库结果 N 条记录进行分页:4guysfromrolla.com/webtech/121298-1.shtml
  • 如果你没有到达“hello world 2”那一行,这意味着你得到了错误,它与分页无关。从您的代码中删除任何 on error resume next 行,再次运行,您很可能会看到错误消息。

标签: asp-classic ado recordset


【解决方案1】:

您是否可以在打开记录集之前声明您的 oRS.pagesize?

这是一个使用 getrows 进行分页的好例子...

<!--VB ADO Constants file.  Needed for the ad... constants we use-->
<!-- #include file="adovbs.inc" -->
<%
' BEGIN USER CONSTANTS
Dim CONN_STRING
Dim CONN_USER
Dim CONN_PASS

' I'm using a DSN-less connection.
' To use a DSN, the format is shown on the next line:
'CONN_STRING = "DSN=DSNName;"

CONN_STRING = "DBQ=" & Server.MapPath("database.mdb") & ";"
CONN_STRING = CONN_STRING & "Driver={Microsoft Access Driver (*.mdb)};"

' This DB is unsecured, o/w you'd need to specify something here
CONN_USER = ""
CONN_PASS = ""

' Our SQL code - overriding values we just set
' Comment out to use Access
CONN_STRING = "Provider=SQLOLEDB;Data Source=10.2.2.133;" _
    & "Initial Catalog=samples;Connect Timeout=15;" _
    & "Network Library=dbmssocn;"
CONN_USER = "samples"
CONN_PASS = "password"
' END USER CONSTANTS


' BEGIN RUNTIME CODE
' Declare our vars
Dim iPageSize       'How big our pages are
Dim iPageCount      'The number of pages we get back
Dim iPageCurrent    'The page we want to show
Dim strOrderBy      'A fake parameter used to illustrate passing them
Dim strSQL          'SQL command to execute
Dim objPagingConn   'The ADODB connection object
Dim objPagingRS     'The ADODB recordset object
Dim iRecordsShown   'Loop controller for displaying just iPageSize records
Dim I               'Standard looping var

' Get parameters
iPageSize = 10 ' You could easily allow users to change this

' Retrieve page to show or default to 1
If Request.QueryString("page") = "" Then
    iPageCurrent = 1
Else
    iPageCurrent = CInt(Request.QueryString("page"))
End If

' If you're doing this script with a search or something
' you'll need to pass the sql from page to page.  I'm just
' paging through the entire table so I just hard coded it.
' What you show is irrelevant to the point of the sample.
'strSQL = "SELECT * FROM sample ORDER BY id;"

' Sept 30, 1999: Code Change
' Based on the non stop questions about how to pass parameters
' from page to page, I'm implementing it so I can stop answering
' the question of how to do it.  I personally think this should
' be done based on the specific situation and is clearer if done
' in the same method on all pages, but it's really up to you.
' I'm going to be passing the ORDER BY parameter for illustration.

' This is where you read in parameters you'll need for your query.
' Read in order or default to id
'If Request.QueryString("order") = "" Then
'   strOrderBy = "id"
'Else
'   strOrderBy = Replace(Request.QueryString("order"), "'", "''")
'End If

' Make sure the input is one of our fields.
strOrderBy = LCase(Request.QueryString("order"))
Select Case strOrderBy
    Case "last_name", "first_name", "sales"
        ' A little pointless, but...
        strOrderBy = strOrderBy
    Case Else
        strOrderBy = "id"
End Select

' Build our SQL String using the parameters we just got.
strSQL = "SELECT * FROM sample ORDER BY " & strOrderBy & ";"

' Some lines I used while writing to debug... uh "test", yeah that's it!
' Left them FYI.
'strSQL = "SELECT * FROM sample WHERE id=1234 ORDER BY id;"
'strSQL = "SELECT * FROM sample;"
'Response.Write "SQL Query: " &  strSQL & "<BR>" & vbCrLf


' Now we finally get to the DB work...
' Create and open our connection
Set objPagingConn = Server.CreateObject("ADODB.Connection")
objPagingConn.Open CONN_STRING, CONN_USER, CONN_PASS

' Create recordset and set the page size
Set objPagingRS = Server.CreateObject("ADODB.Recordset")
objPagingRS.PageSize = iPageSize

' You can change other settings as with any RS
'objPagingRS.CursorLocation = adUseClient
objPagingRS.CacheSize = iPageSize

' Open RS
objPagingRS.Open strSQL, objPagingConn, adOpenStatic, adLockReadOnly, adCmdText

' Get the count of the pages using the given page size
iPageCount = objPagingRS.PageCount

' If the request page falls outside the acceptable range,
' give them the closest match (1 or max)
If iPageCurrent > iPageCount Then iPageCurrent = iPageCount
If iPageCurrent < 1 Then iPageCurrent = 1

' Check page count to prevent bombing when zero results are returned!
If iPageCount = 0 Then
    Response.Write "No records found!"
Else
    ' Move to the selected page
    objPagingRS.AbsolutePage = iPageCurrent

    ' Start output with a page x of n line
    %>
    <p>
    <font size="+1">Page <strong><%= iPageCurrent %></strong>
    of <strong><%= iPageCount %></strong></font>
    </p>
    <%
    ' Spacing
    Response.Write vbCrLf

    ' Continue with a title row in our table
    Response.Write "<table border=""1"">" & vbCrLf

    ' Show field names in the top row
    Response.Write vbTab & "<tr>" & vbCrLf
    For I = 0 To objPagingRS.Fields.Count - 1
        Response.Write vbTab & vbTab & "<th>"
        Response.Write objPagingRS.Fields(I).Name
        Response.Write "</th>" & vbCrLf
    Next 'I
    Response.Write vbTab & "</tr>" & vbCrLf

    ' Loop through our records and ouput 1 row per record
    iRecordsShown = 0
    Do While iRecordsShown < iPageSize And Not objPagingRS.EOF
        Response.Write vbTab & "<tr>" & vbCrLf
        For I = 0 To objPagingRS.Fields.Count - 1
            Response.Write vbTab & vbTab & "<td>"
            Response.Write objPagingRS.Fields(I)
            Response.Write "</td>" & vbCrLf
        Next 'I
        Response.Write vbTab & "</tr>" & vbCrLf

        ' Increment the number of records we've shown
        iRecordsShown = iRecordsShown + 1
        ' Can't forget to move to the next record!
        objPagingRS.MoveNext
    Loop

    ' All done - close table
    Response.Write "</table>" & vbCrLf
End If

' Close DB objects and free variables
objPagingRS.Close
Set objPagingRS = Nothing
objPagingConn.Close
Set objPagingConn = Nothing


' Show "previous" and "next" page links which pass the page to view
' and any parameters needed to rebuild the query.  You could just as
' easily use a form but you'll need to change the lines that read
' the info back in at the top of the script.
If iPageCurrent > 1 Then
    %>
    <a href="db_paging.asp?page=<%= iPageCurrent - 1 %>&order=<%= Server.URLEncode(strOrderBy) %>">[&lt;&lt; Prev]</a>
    <%
End If

' You can also show page numbers:
For I = 1 To iPageCount
    If I = iPageCurrent Then
        %>
        <%= I %>
        <%
    Else
        %>
        <a href="db_paging.asp?page=<%= I %>&order=<%= Server.URLEncode(strOrderBy) %>"><%= I %></a>
        <%
    End If
Next 'I

If iPageCurrent < iPageCount Then
    %>
    <a href="db_paging.asp?page=<%= iPageCurrent + 1 %>&order=<%= Server.URLEncode(strOrderBy) %>">[Next &gt;&gt;]</a>
    <%
End If

' END RUNTIME CODE
%>

【讨论】:

    【解决方案2】:

    尝试将您的 rs.open 行更改为:

    rs.Open strSQL, Conn, 3, 1, &H0001
    

    下面是函数调用和参数的细分: recordsetobject.Open Source、ActiveConnection、CursorType、LockType、选项

    3 - adOpenStatic

    1 - adLockReadOnly

    &H0001 - adCmdText

    我从我的一些旧代码中提取了这个。我不记得为什么需要这种参数组合,但这是实现分页所必需的。

    无限记录听起来不错,但我会设置一个限制,即使它很嗨。

    如果您没有得到“Hello World 2”输出,是不是有错误?这也会很有帮助。

    【讨论】:

    • 我现在正在尝试您的解决方案。我试图错误捕获 rs.open 以查看是否报告了错误。脚本在那个时候就挂了。
    • 你至少应该得到一个脚本超时。您是否在 IIS 中打开了向客户端发送错误?我假设您没有在生产服务器上测试这样的东西。此外,如果您使用的是 IE,则需要关闭“显示友好的错误消息”。
    • 我的错误被发送到 Windows 日志。我根本没有得到任何回报。我不使用 IE。
    【解决方案3】:

    分页就是为了这个,分页。

    您这里的代码不足以实现这一点,但不管代码如何,您为什么要尝试返回 1000 行数据????

    没有人会读取 1000 行数据,这可能会导致性能非常缓慢。

    【讨论】:

    • 我有一个客户想要一份订单和客户报告。如果需要,他们将读取 10000 行数据,并且他们有一个程序来解析和分析数据。是的,它很慢,但是我们的应用程序中没有创建它的接口。
    • 我真的不认为社区决定如何使用代码,除了开发人员自己。如果他的处境与最初的开发者不同,他可能会认为它没有用。
    【解决方案4】:

    使用以下代码,我从经典 ASP 中的 sql server 表中返回了 4000 多行。它没有使用相同的方法,但也不受限制。

    strconnect = "DRIVER={SQL Server};SERVER=****;DATABASE=****;UID=****;PWD=****"
    set conn=server.createobject("adodb.connection")
    conn.open strconnect
    set rs = conn.execute("select firstname from users")
    if not rs.eof then
      f_Array = rs.getrows
    end if
    rs.close
    set rs = nothing
    conn.close
    set conn = nothing
    
    for x = 0 to ubound(f_Array, 2)
      response.write (x+1) & ". " & f_Array(0,x) & "<br />"
    next
    

    【讨论】:

    • 我打开分页的唯一原因是为了超出本文的 1000 行限制:link 和许多其他人。
    • 我想我把这个答案贴错了。很抱歉。由于我在这里,我正在使用我刚刚运行的代码编辑我的答案,并使用经典 asp 从 sql 服务器返回 4000 多行。我不知道你是否会发现它有帮助,但确实如此。 :)
    猜你喜欢
    • 2010-09-24
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多