使用Active Server Page将记录复制到另一个记录集

时间:2010-11-12 13:21:06

标签: asp-classic sql-server-ce

我正在使用Classic ASP进行编程。我正在尝试进行分页。我的后端是SQL CE 3.5。不幸的是,它不支持SQL Query中的分页(如sql server中的row_number())。

所以我选择ASP Paging。但是当我问记录集时,通过设置rs.PageSize和rs.AbsolutePage以及所有记录来给我前10条记录,它给了我所有记录。因此,我计划仅将结果记录集中的前10行复制到另一个新记录集。所以我编码如下:


Set rsTemp = CopyRecordsetStructure(objRs)
rsTemp.Open
iRecordsShown = 0
Set objFields = objRs.Fields
intFieldsCount = objFields.Count-1 
Do While iRecordsShown < intPageSize And Not objRs.EOF
 rsTemp.AddNew
 For Idx = 0 To intFieldsCount
  rsTemp.Fields(Idx).Value = objRs.Fields(Idx).Value
 Next
 rsTemp.Update
 iRecordsShown = iRecordsShown + 1
 objRs.MoveNext
Loop

Public Function CopyRecordsetStructure(ByVal rs) Dim rsTemp Set rsTemp = CreateObject("ADODB.Recordset") Set objFields = rsTemp.Fields intFieldCount = objFields.Count - 1 For Idx = 0 To intFieldCount rsTemp.Fields.Append objFields(Idx).Name, _ objFields(Idx).Type, _ objFields(Idx).DefinedSize Next Set CopyRecordsetStructure = rsTemp End Function

问题是我无法打开“rsTemp”。它引发了我的错误


The connection cannot be used to perform this operation. It is either closed or invalid in this context.

如果我使用一些虚拟查询和连接,它就不起作用。

请帮助将记录从一个记录集复制到另一个新记录集。

提前致谢 内甚

2 个答案:

答案 0 :(得分:0)

不确定,但这看起来不对

Set objFields = rsTemp.Fields

不应该是

Set objFields = rs.Fields

答案 1 :(得分:0)

使用上述注释中的注释并将其更新为将objFields = rs.Fields 更新为:

<强>用法:

Dim rsTemp
Set rsTemp = CopyRecordset(rsPadicon)

更新代码

Public Function CopyRecordset(rs)

    Dim rsTemp, objFields, intFieldsCount, intPageSize
    Set rsTemp = CopyRecordsetStructure(rs)
    rsTemp.Open

    Set objFields = rs.Fields
    intFieldsCount = objFields.Count-1 

    response.write("<li> rs.RecordCount  :" & rs.RecordCount  & "</li>")
    ' response.write("<li> intFieldsCount  :" & intFieldsCount & "</li>")

    rs.MoveFirst 
    Do While Not rs.EOF
        rsTemp.AddNew

        Dim i
        For i = 0 to intFieldsCount 'use i as a counter
            ' response.write("<li> Name :" & rs.Fields(i).Name & "</li>")
            ' response.write("<li> Value :" & rs.Fields(i).Value & "</li>")
            if Not IsNull(rs.Fields(i).Value) then 
                rsTemp.Fields(i).Value = rs.Fields(i).Value
            End if
        Next

        rsTemp.Update

        rs.MoveNext
    Loop

    Set CopyRecordset = rsTemp


End Function


Public Function CopyRecordsetStructure(ByVal rs)
     Dim rsTemp, objFields, intFieldCount, Idx
     Set rsTemp = CreateObject("ADODB.Recordset")
     Set objFields = rs.Fields
     intFieldCount = objFields.Count - 1
     For Idx = 0 To intFieldCount
      rsTemp.Fields.Append objFields(Idx).Name, _
            objFields(Idx).Type, _
            objFields(Idx).DefinedSize
     Next
     Set CopyRecordsetStructure = rsTemp
End Function