我在 ASP 页面中有一个查询。在某些条件下,我获得的记录集必须打印在 3 个不同的表中。所以,为了避免执行 3 次几乎相同的查询,我决定在记录集中搜索我需要的结果......所以我需要做两次 RS.MoveFirst。 但是……当我使用 SQL Profiler 进行分析时,我发现 MoveFirst 操作重新执行了我的查询……这正是我想要避免的。 我怎样才能缓存结果并只在记录集中移动?
最佳答案
使用断开连接的记录集
Const adOpenStatic = 3
Const adUseClient = 3
Const adLockOptimistic = 3
Dim conn: Set conn = Server.CreateObject("ADODB.Connection")
conn.Open sYourConnectionString
Dim rs : Set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorLocation = adUseClient
rs.Open sYourSQL, conn, adOpenStatic, adLockOptimistic
Set rs.ActiveConnection = Nothing
conn.close
'' // You can now roam around the recordset with .MoveFirst, .MoveNext etc without
'' // incurring any further hits on the DB.
请注意,如果您要为 sql 提供参数,则需要在连接和记录集之间使用 ADODB.Command
对象(不要试图使用字符串连接)。原理仍然相同,使用客户端游标位置和静态记录集,然后分离并关闭连接。
关于asp-classic - 如何避免在 MoveFirst 上重新执行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1244194/