65.9K
CodeProject 正在变化。 阅读更多。
Home

如何将 ADO RecordSet 转换为数组。

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.95/5 (6投票s)

2005年10月20日

1分钟阅读

viewsIcon

150445

如何将 ADO RecordSet 转换为数组。

引言

通常需要将 SQL 查询检索到的记录集转换为数组。这可以通过 Recordset 对象的 GetRows 方法来实现,该方法会返回一个变体数组。

 

过程如下:

 

Dim dbA As ADODB.Connection

Dim rsA As ADODB.Recordset

 

    Set dbA = New ADODB.Connection

    Set rsA = New ADODB.Recordset

    dbA.Open "DSN=tstDSN;UID=sa;PWD=pass;"

 

Dim csSql As String

csSql = "SELECT    * From EmployeeTable”

 

            Set rsA = dbA.Execute(csSql)

            If rsA.EOF = True And rsA.BOF = True Then

                GoTo exithandler

            End If

 

'将其转换为数组。

Dim nNoOfReords as Integer

arrRecordArray = rsA.GetRows

nNoOfReords= UBound(vRecordArray, 2)+1

 

‘现在 arrRecordArray 包含数组格式的记录集。

 

考虑一个包含字段 empID、empName、empAge 的 employee 表,如下所示:

 

empID              empName        empAge

----------------------------------------------

1                      Smith                24

2                      Baiju                26

3                      Ragi                 26

4                      Pramod            23

5                      Bincy                25

----------------------------------------------

那么变体数组的结构将是:

 

arrRecordArray(0,0)=1,  arrRecordArray(1,0)=Smith, arrRecordArray(2,0)=24

arrRecordArray(0,1)=2,  arrRecordArray(1,1)=Baiju, arrRecordArray(2,1)=26

 

即,第一个参数指定字段索引,第二个参数指定记录索引。

 

返回的记录数可以通过以下方式找到:

nNoOfReords= UBound(vRecordArray, 2)+1

 

希望这些信息对您有所帮助。

 

作者:Smith.S.Raj

软件工程师

M-Squared, Technopark。

 

© . All rights reserved.