0

So, you have a recordset created like this:

Set rs = Server.CreateObject("ADODB.Recordset")

And you fill it like this:

rs.Open queryString, AuthConn, adOpenKeyset, adLockReadOnly

My question is, I want a second recordset that is a subset of the first (rs) recordset, can you do this in classic asp

Set rs2 = Server.CreateObject("ADODB.Recordset")

My immediate guess is that it would be something like this

rs2.Open queryString, rs, adOpenKeyset, adLockReadOnly

Why you ask? Well we have an older site that we are updating and adding new features too and rather than change a LOT of code I was wondering if I could be sneaky and use a setset of an already created (large) recordset, to save on another query to the db etc. Just wondering if it can be done.

Thanks,

4

3 回答 3

3

You can use the Clone method to create a duplicate recordset, then use Filter to reduce the dataset to what you're interested in. For example:

Dim rs, rs2
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open queryString, AuthConn, adOpenKeyset, adLockReadOnly

Set rs2 = rs.Clone()
rs2.Filter = "Field1 = 'foo'"

The string form of Filter is basic; it's pretty much <Field> <op> <value>. You can combine multiple expressions using AND and OR, but even that has some limitations (see the documentation link for the full details).

For more complex filtering, you can pass the Filter property an array of Bookmark objects. In this case, you loop through the recordset (or a clone of the recordset), testing each record by whatever complex criteria you have. If the record passes the test, you save its Bookmark to an array or other collection. Then, you can set the Filter property to your array of Bookmarks and you have a custom-filtered recordset.

'Note that I haven't tested this code
Dim rs, rs2, bookmarks
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open queryString, AuthConn, adOpenKeyset, adLockReadOnly

Set rs2 = rs.Clone()
bookmarks = Array()
Do Until rs2.EOF
    If rs2("Field1") = 2 * rs2("Field2") Then
        ReDim Preserve bookmarks(UBound(bookmarks) + 1)
        bookmarks(UBound(bookmarks)) = rs2.Bookmark
    End If
    rs2.MoveNext
Loop

rs2.Filter = bookmarks
' Now rs2 contains only records where Field1 = 2*Field2

You can use this same technique to get unique values (aka DISTINCT) by using a Dictionary object to store the unique key values. Doing a DISTINCT on multiple fields is a bit trickier. What I've done is the past is to combine the multiple fields using a separator that won't be in the data (such as a pipe |). That's not always possible, though.

于 2013-08-23T06:16:04.083 回答
0

显然,您可以创建第二个查询和记录集并在“do while not rs.eof............loop”中一遍又一遍地运行它 - 我猜这就是你想要避免的。

您可能想看看 Datashaping。这里有一篇关于它的文章。它很旧,但这是经典的 ASP

http://www.4guysfromrolla.com/webtech/092599-1.shtml

MSDN 上也曾经有一个页面,叫做“使用数据形状创建分层记录集”。您会在 Google 上找到大量指向它的链接,但它们现在都将您带到 .net 开发人员资源上的 404 页面:(

于 2013-08-23T09:55:31.550 回答
0

记录集对象只能由“连接”对象打开,您不能如上所示将连接对象替换为另一个记录集对象,但是如果您修改查询字符串对象并通过您自己的查询打开所需的子集,您就可以实现这一点。

如果您在制作子集时遇到困难,请发布查询字符串。

于 2013-08-23T02:31:47.747 回答