日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 數據庫 > access > 在Recordset對象中查詢記錄的方法

在Recordset對象中查詢記錄的方法

來源:程序員人生   發布時間:2013-11-04 03:26:14 閱讀次數:2841次
無論是 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 FilesMicrosoft officeOfficeSamplesNorthwind.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 FilesMicrosoft OfficeOfficeSamplesNorthwind.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 FilesMicrosoft OfficeOfficeSamplesNorthwind.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 FilesMicrosoft OfficeOfficeSamplesNorthwind.mdb”, _

“PrimaryKey”, “Order Details”, Array(10255,16), ”Quantity”

( 譯者注:如果你安裝的是簡體中文版要將示例中引用的相應的字段名改成中文 )

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 欧美激情精品久久久久久变态 | 国产精品国产三级国产aⅴ原创 | 一区二区三区四区不卡视频 | 一区二区视频网站 | 欧美人体一区二区三区 | 日韩区欧美久久久无人区 | 最近中文字幕mv免费高清在线 | 日韩一区精品视频 | 欧美日韩一区三区 | 成人免费在线观 | 成人在线视频免费观看 | 欧美三级欧美成人高清 | 久久精品免费观看 | 国产男女视频 | jizz在线免费观看 | 欧美二区在线观看 | 久久国产成人精品 | 国产精品久久亚洲7777 | 在线看污 | 毛片av在线 | 91亚洲精选| 国产一区二区三区在线视频 | 性一交一乱一乱一视频96 | av在线中文 | 日韩精品久久久久 | 看a网站| 成人国产精品免费观看视频 | 99精品99| 久久综合热 | 欧美日韩在线播放 | 一及毛片视频 | 综合伊人| 精品久久久久久久久久久久久久久久久久 | 欧洲精品一区二区 | 久久国精品 | 91年国产电影大全免费观看 | 欧美无乱码久久久免费午夜一区 | 欧美日韩在线播放 | 久久国产精品一区二区三区 | 久久55| 国产三级欧美三级 |