我正在为我们的遗留程序将一些旧的遗留访问代码转换为SQL,我们在其中根据数千条记录的数据创建报告。我们使用多个记录集来访问不同的记录/表。以前(在访问中),我们使用Recordset.Seek函数在记录集中找到了所需的记录。这样做可以以秒的方式获取我们想要的数据。
使用ADO Recordset而不是DAO将其转换为使用SQL时,我发现大多数人建议只使用将返回所需记录的查询将记录集设置为等于新记录集。到目前为止,这是最有效的方法,但这仍然需要2分钟才能获得访问时间相同的数据。
我看到ADO Recordset也有.seek功能,但我在尝试实现它以比较速度/准确性时遇到了麻烦。如果这是一个更好的方法,我如何创建/设置记录集的索引,以及如何知道索引的名称?
谢谢!
答案 0 :(得分:3)
您只能将Seek
用于JET OLEDB提供程序的服务器端记录集。您可以使用类似Seek
的内容来测试记录集中是否有rs.Supports(adSeek)
。
我通常用客户端记录集做的是建立内存索引"手动"有这样的功能:
Public Function InitIndexCollection( _
rs As Recordset, _
sFld As String, _
Optional Fld2 As String, _
Optional Fld3 As String, _
Optional ByVal HasDuplicates As Boolean, _
Optional RetVal As Collection) As Collection
Const FUNC_NAME As String = "InitIndexCollection"
Dim oFld As ADODB.Field
Dim oFld2 As ADODB.Field
Dim oFld3 As ADODB.Field
Dim vBmk As Variant
Dim pCol As Collection '-- IVbCollection that doesn't raise duplicate error on Add
Set RetVal = New Collection
With rs
If .RecordCount > 0 Then
On Error Resume Next
vBmk = rs.Bookmark
On Error GoTo 0
.MoveFirst
Set oFld = .Fields(sFld)
If LenB(Fld2) <> 0 Then
Set oFld2 = .Fields(Fld2)
End If
If LenB(Fld3) <> 0 Then
Set oFld3 = .Fields(Fld3)
End If
If HasDuplicates Then
On Error Resume Next
Set pCol = RetVal
If oFld2 Is Nothing Then
Do
pCol.Add .Bookmark, CStr(oFld.Value)
.MoveNext
Loop While Not .EOF
ElseIf oFld3 Is Nothing Then
Do
pCol.Add .Bookmark, CStr(oFld.Value) & "#" & CStr(oFld2.Value)
.MoveNext
Loop While Not .EOF
Else
Do
pCol.Add .Bookmark, CStr(oFld.Value) & "#" & CStr(oFld2.Value) & "#" & CStr(oFld3.Value)
.MoveNext
Loop While Not .EOF
End If
On Error GoTo 0
Else
If oFld2 Is Nothing Then
Do
RetVal.Add .Bookmark, CStr(oFld.Value)
.MoveNext
Loop While Not .EOF
ElseIf oFld3 Is Nothing Then
Do
RetVal.Add .Bookmark, CStr(oFld.Value) & "#" & CStr(oFld2.Value)
.MoveNext
Loop While Not .EOF
Else
Do
RetVal.Add .Bookmark, CStr(oFld.Value) & "#" & CStr(oFld2.Value) & "#" & CStr(oFld3.Value)
.MoveNext
Loop While Not .EOF
End If
End If
If Not IsEmpty(vBmk) Then
.Bookmark = vBmk
End If
End If
End With
Set InitIndexCollection = RetVal
End Function
然后你可以像{/ p>一样快速寻找Bookmark
财产
Dim rsItems As Recordset
Dim cIndex As Collection
Set cIndex = InitIndexCollection(rsItems, "Code")
...
'--- loop on other rs'
Do While Not rs.EOF
...
rsItems.Bookmark = cIndex(rs!ItemCode.Value) '--- this is seek'
...
rs.MoveNext
Loop