首页 > 开发 > 综合 > 正文

在Recordset对象中查询记录的方法

2024-07-21 02:12:33
字体:大 中 小
来源:转载
供稿:网友
无论是 dao 还是 ado 都有两种从 recordset 对象中查询记录的方法: find 方法和 seek 方法。在这两种方法中可以让你指定条件进行查询与其相应的记录 , 一般而言,在相同条件下, seek 方法提供了比 find 方法更好的性能,因为 seek 方法是基于索引的。因为这个原因基本提供者必须支持 recordset 对象上的索引,可以用 supports ( adseek ) 方法确定基本提供者是否支持 seek ,用 supports ( adindex ) 方法确定提供者是否支持索引。(例如, ole db provider for microsoft jet 支持 seek 和 index 。),请将 seek 方法和 index 属性结合使用。如果 seek 没有找到所需的行,将不会产生错误,该行将被放在 recordset 的结尾处。执行此方法前,请先将 index 属性设置为所需的索引。此方法只受服务器端游标支持。如果 recordset 对象的 cursorlocation 属性值为 aduseclient ,将不支持 seek 。只有当 commandtypeenum 值为 adcmdtabledirect 时打开 recordset 对象,才可以使用此方法。

用 ado find 方法

dao 包含了四个“ find ”方法: findfirst,findlast,findnext 和 findprevious .

dao 方法 ado find 方法

下面的一个例子示范了如何用 ado find 方法查询记录:

sub findrecord(strdbpath as string, _

strtable as string, _

strcriteria as string, _

strdisplayfield as string)

' this procedure finds a record in the specified table by

' using the specified criteria.

' for example, to use this procedure to find records

' in the customers table in the northwind database

' that have " usa " in the country field, you can

' use a line of code like this:

' findrecord _

' "c:/program files/microsoft office/office/samples/northwind.mdb", _

' "customers", "country=' usa '", "customerid"

dim cnn as adodb.connection

dim rst as adodb.recordset

' open the connection object.

set cnn = new adodb.connection

with cnn

.provider = "microsoft.jet.oledb.4.0"

.open strdbpath

end with

set rst = new adodb.recordset

with rst

' open the table by using a scrolling

' recordset object.

.open source:=strtable, _

activeconnection:=cnn, _

cursortype:=adopenkeyset, _

locktype:=adlockoptimistic

' find the first record that meets the criteria.

.find criteria:=strcriteria, searchdirection:=adsearchforward

' make sure record was found (not at end of file).

if not .eof then

' print the first record and all remaining

' records that meet the criteria.

do while not .eof

debug.print .fields(strdisplayfield).value

' skip the current record and find next match.

.find criteria:=strcriteria, skiprecords:=1

loop

else

msgbox "record not found"

end if

' close the recordset object.

.close

end with

' close connection and destroy object variables.

cnn.close

set rst = nothing

set cnn = nothing

end sub

例如,用用这个过程查询“罗期文商贸”示例数据库中“客户”表的“国家”等于 usa 的记录,可以使用下面的代码:

findrecord “c:/program files/microsoft office/office/samples/northwind.mdb”, _

“customers”, “country=' usa '”, ”customerid”

( 译者注:如果你安装的是简体中文版要将相应的字段名改成中文 )

用 ado seek 方法

因为 ado seek 方法使用 index ,最好是在用这个方法之前指定一个索引,可是,如果你没有指定索引, jet database engine 将用主键。

如果你需要从多个字段中指定值做为搜索条件,可以用 vba 中的 array 函数传递这些值到参数 keyvalues 中去。如果你只需要从一个字段中指定值做为搜索条件,则不需要用 array 函数传递。

象 find 方法一样,你可以用 bof 或者 eof 属性测试是否查询到记录。

下面的一个例子显示了如何用 ado seek 方法查询记录:

sub seekrecord(strdbpath as string, _

strindex as string, _

strtable as string, _

varkeyvalues as variant, _

strdisplayfield as string)

' this procedure finds a record by using

' the specified index and key values.

' for example, to use the primarykey index to

' find records in the order details table in the

' northwind database where the orderid field is

' 10255 and productid is 16, and then display the

' value in the quantity field, you can use a line

' of code like this:

' seekrecord _

' "c:/program files/microsoft office/office/samples/northwind.mdb", _

' "primarykey", "order details", array(10255, 16), "quantity"

dim cnn as adodb.connection

dim rst as adodb.recordset

' open the connection object.

set cnn = new adodb.connection

with cnn

.provider = "microsoft.jet.oledb.4.0"

.open strdbpath

end with

set rst = new adodb.recordset

with rst

' select the index used to order the

' data in the recordset.

.index = strindex

' open the table by using a scrolling

' recordset object.

.open source:=strtable, _

activeconnection:=cnn, _

cursortype:=adopenkeyset, _

locktype:=adlockoptimistic, _

options:=adcmdtabledirect

' find the order where orderid = 10255 and

' productid = 16.

.seek keyvalues:=varkeyvalues, seekoption:=adseekfirsteq

' if a match is found, print the value of

' the specified field.

if not .eof then

debug.print .fields(strdisplayfield).value

end if

' close the recordset object.

.close

end with

' close connection and destroy object variables.

cnn.close

set rst = nothing

set cnn = nothing

end sub

例如,用主键索引查询“罗期文商贸”示例数据库中“订单明细”表的“订单编号”等于 10255 并且产品编号等于 16 的 ” 数量 ” 的值,可以使用下面的代码:

seekrecord “c:/program files/microsoft office/office/samples/northwind.mdb”, _

“primarykey”, “order details”, array(10255,16), ”quantity”

( 译者注:如果你安装的是简体中文版要将示例中引用的相应的字段名改成中文 )



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表