怎么在ADO中使用索引(seek)?

linfeng 2000-07-06 06:45:00
...全文
388 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
okok 2000-07-25
  • 打赏
  • 举报
回复
以下是adohelp中的说明:
Seek 方法


搜索 Recordset 的索引,快速定位与指定值相匹配的行,并将当前行更改为该行。

语法

recordset.Seek KeyValues, SeekOption

参数

KeyValues VARIANT 值的数组。索引由一个或多个列组成,而数组包含与每个对应列进行比较的值。

SeekOption SeekEnum 只值,指定在索引的列和对应的 KeyValues 之间进行的比较的类型。可以是如下某个比较常量:

常量 说明
AdSeekAfterEQ 查找等于 KeyValues 的关键字,或仅在已经匹配过的位置之后进行查找。
AdSeekAfter 仅在已经有过与 KeyValues 匹配的位置之后进行查找。
AdSeekBeforeEQ 查找等于 KeyValues 的关键字,或仅在已经匹配过的位置之前进行查找。
AdSeekBefore 仅在已经有过与 KeyValues 匹配的位置之前进行查找。
AdSeekFirstEQ 查找等于 KeyValues 的第一个关键字。
AdSeekLastEQ 查找等于 KeyValues 的最后一个关键字。


说明

如果基本提供者支持对 Recordset 对象使用索引,请结合 Index 属性使用 Seek 方法。请使用 Supports (adIndex) 方法判断基本提供者是否支持索引。

如果 Seek 没有找到想要的行,则不发生错误,并且行被定位于 EOF。请在执行该方法之前,将 Index 属性设置为所需索引。

该方法只能用于当 Recordset 对象的 CursorLocation 属性的值不是 adUseClient 时。
另:
Index 属性


指示对 Recordset 对象当前生效的索引的名称。

设置和返回值

设置或返回字符串值,该值为索引名。

说明

由 Index 属性命名的索引必须针对基本表基本 Recordset 对象已在前面声明过。即索引必须已在程序中声明为 ADOX Index 对象,或在创建基本表时声明。

如果无法设置索引,则会发生运行时错误。无法在 WillRecordsetChange 或 RecordsetChangeComplete 事件处理程序内设置 Index 属性。如果 Recordset 正在执行操作,也无法对它进行设置。如果 Recordset 是关闭的,则总能成功设置 Index 属性,但如果基本提供者不支持索引,则 Recordset 将无法成功打开,或者索引将无法使用。

如果可以设置索引,则可以更改当前行的位置。这将导致对 AbsolutePosition 属性的更新,并产生 WillRecordsetChange、RecordsetChangeComplete、WillMove 和 MoveComplete 事件。

如果可以设置索引,而 LockType 属性是 adLockPessimistic 或 adLockOptimistic,那么,将执行隐式 UpdateBatch 操作,并释放当前的和受影响的组。任何现有的 filter 被释放,并且当前行位置更改为重排序后 Recordset 的第一行。

Index 属性与 Seek 方法连通使用。如果基本提供者不支持 Index 属性和 Seek 方法,请考虑使用 Find 方法替代。使用 Supports(adIndex) 方法可判定 Recordset 对象是否支持索引。

尽管二者均处理索引,但内置的 Index 属性与动态的 Optimize 属性无关。

注:我觉得帮助文件中的说明可以满足你的要求。
huntout 2000-07-25
  • 打赏
  • 举报
回复
看看這個例子對你有沒有用?

Sub SeekX()

Dim dbsNorthwind As Database
Dim rstProducts As Recordset
Dim intFirst As Integer
Dim intLast As Integer
Dim strMessage As String
Dim strSeek As String
Dim varBookmark As Variant

Set dbsNorthwind = OpenDatabase("Northwind.mdb")
' You must open a table-type Recordset to use an index,
' and hence the Seek method.
Set rstProducts = _
dbsNorthwind.OpenRecordset("Products", dbOpenTable)

With rstProducts
' Set the index.
.Index = "PrimaryKey"

' Get the lowest and highest product IDs.
.MoveLast
intLast = !ProductID
.MoveFirst
intFirst = !ProductID

Do While True
' Display current record information and ask user
' for ID number.
strMessage = "Product ID: " & !ProductID & vbCr & _
"Name: " & !ProductName & vbCr & vbCr & _
"Enter a product ID between " & intFirst & _
" and " & intLast & "."
strSeek = InputBox(strMessage)

If strSeek = "" Then Exit Do

' Store current bookmark in case the Seek fails.
varBookmark = .Bookmark

.Seek "=", Val(strSeek)

' Return to the current record if the Seek fails.
If .NoMatch Then
MsgBox "ID not found!"
.Bookmark = varBookmark
End If
Loop

.Close
End With

dbsNorthwind.Close

End Sub
linfeng 2000-07-14
  • 打赏
  • 举报
回复
seek
neulf 2000-07-12
  • 打赏
  • 举报
回复
什么索引?详细点!
zcom 2000-07-11
  • 打赏
  • 举报
回复
在ADO中完全不用SEEK!!!
linfeng 2000-07-10
  • 打赏
  • 举报
回复
hi
linfeng 2000-07-08
  • 打赏
  • 举报
回复
我见ado有一个方法seek,但不知怎么用。
如我有一个MDB库有一个表main中建了一个索引,在ado中能用这个索引吗?
jing 2000-07-07
  • 打赏
  • 举报
回复
什么意思?
如果排序 sort
linfeng 2000-07-07
  • 打赏
  • 举报
回复
我见ado有一个方法seek,但不知怎么用。
如我有一个MDB库有一个表main中建了一个索引,在ado中能用这个索引吗?
linfeng 2000-07-06
  • 打赏
  • 举报
回复
能不能具体一些,给点代码提示.
subzero 2000-07-06
  • 打赏
  • 举报
回复
对应某个字段使用索引

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧