GetRows 方法范例 (VB)

GetRows 方法范例 (VB)

GetRows 方法范例 (VB)

本范例使用 GetRows 方法从 Recordset 中检索指定数目的行,并用结果数据填充一个数组。在两种情况下,GetRows 方法返回的行将少于所需的数目:一种情况是到达了 EOF,另一种情况是 GetRows 试图检索被其他用户删除的记录。仅在发生第二种情况时,该函数才返回 False。运行此过程需要 GetRowsOK 函数。

GetRows 方法
将 Recordset 对象的多个记录检索到数组中。

语法
array = recordset.GetRows( Rows, Start, Fields )
返回值
返回 Variant,其值为二维数组。

参数
Rows
可选。GetRowsOptionEnum 值,指示要检索的记录数。默认值为 adGetRowsRest。
Start
可选。String 值或 Variant,计算 GetRows ***作开始处的记录的书签。还可以使用 BookmarkEnum 值。
Fields
可选。Variant,表示单个字段名或序号位置,或者字段名数组或序号位置编号。ADO 仅返回这些字段中的数据。
说明
使用 GetRows 方法将 Recordset 中的记录复制到二维数组中。第一个下标标识字段,第二个下标标识记录编号。当 GetRows 方法返回数据时,array 变量将自动调整到正确大小。

如果未指定 Rows 参数的值,GetRows 方法将自动检索 Recordset 对象中的所有记录。如果请求的记录多于可用的记录,GetRows 仅返回可用的记录数目。

如果 Recordset 对象支持书签,可以通过在 Start 参数中传递该记录的 Bookmark 属性的值来指定 GetRows 方法从哪一个记录开始检索数据。

如果要限制 GetRows 调用返回的字段,可以在 Fields 参数中传递单个字段名/编号,或者字段名/编号的数组。

在调用 GetRows 后,下一个未读取的记录成为当前记录。如果没有其他记录,EOF 属性将被设置为 True。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'BeginGetRowsVB
 
     'To integrate this code
     'replace the data source and initial catalog values
     'in the connection string
     
Public Sub Main()
     On Error GoTo ErrorHandler
 
      ' connection and recordset variables
     Dim rstEmployees As ADODB.Recordset
     Dim Cnxn As ADODB.Connection
     Dim strSQLEmployees As String
     Dim strCnxn As String
     ' array variable
     Dim arrEmployees As Variant
     ' detail variables
     Dim strMessage As String
     Dim intRows As Integer
     Dim intRecord As Integer
     
     ' open connection
     Set Cnxn = New ADODB.Connection
     strCnxn = "Provider='sqloledb';Data Source='MySqlServer';" & _
         "Initial Catalog='Pubs';Integrated Security='SSPI';"
     Cnxn.Open strCnxn
     
     ' open recordset client-side to enable RecordCount
     Set rstEmployees = New ADODB.Recordset
     strSQLEmployees = "SELECT fName, lName, hire_date FROM Employee ORDER BY lName"
     rstEmployees.Open strSQLEmployees, Cnxn, adOpenStatic, adLockReadOnly, adCmdText
     
     ' get user input for number of rows
     Do
         strMessage = "Enter number of rows to retrieve:"
         intRows = Val(InputBox(strMessage))
         
           ' if bad user input exit the loop
         If intRows <= 0 Then
             MsgBox "Please enter a positive number" , vbOKOnly, "Not less than zero!"
         ' if number of requested records is over the total
         ElseIf intRows > rstEmployees.RecordCount Then
             MsgBox "Not enough records in Recordset to retrieve " & intRows & " rows." , _
             vbOKOnly, "Over the available total"
         Else
             Exit Do
         End If
     Loop
     
       ' else put the data in an array and print
     arrEmployees = rstEmployees.GetRows(intRows)
     
     Dim x As Integer , y As Integer
     
     For x = 0 To intRows - 1
         For y = 0 To 2
             Debug.Print arrEmployees(y, x) & " " ;
         Next y
         Debug.Print vbCrLf
     Next x
     
     ' clean up
     rstEmployees.Close
     Cnxn.Close
     Set rstEmployees = Nothing
     Set Cnxn = Nothing
     Exit Sub
     
ErrorHandler:
     ' clean up
     If Not rstEmployees Is Nothing Then
         If rstEmployees.State = adStateOpen Then rstEmployees.Close
     End If
     Set rstEmployees = Nothing
     
     If Not Cnxn Is Nothing Then
         If Cnxn.State = adStateOpen Then Cnxn.Close
     End If
     Set Cnxn = Nothing
     
     If Err <> 0 Then
         MsgBox Err.Source & "-->" & Err.Description, , "Error"
     End If
End Sub
'EndGetRowsVB

http://517sou.net/archives/getrows-%e6%96%b9%e6%b3%95%e8%8c%83%e4%be%8b-vb/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`getRows()`是Spark SQL中的一个方法,用于获取查询结果集合。在Java中使用`getRows()`方法可以通过以下步骤: 1. 首先获取到SparkSession对象,可以通过以下代码获取: ```java SparkSession sparkSession = SparkSession.builder() .appName("JavaSparkSQLExample") .config("spark.some.config.option", "some-value") .getOrCreate(); ``` 2. 通过SparkSession对象创建Dataset或DataFrame,例如: ```java Dataset<Row> df = sparkSession.read().json("path/to/json"); ``` 3. 执行查询操作,例如: ```java df.createOrReplaceTempView("people"); Dataset<Row> sqlDF = sparkSession.sql("SELECT * FROM people"); ``` 4. 调用`getRows()`方法获取查询结果集,例如: ```java Row[] rows = (Row[])sqlDF.collect(); ``` 注意,`getRows()`方法返回的是一个数组,其中每个元素都是一个`Row`对象,表示一行数据。可以通过`Row`对象的`get*()`方法获取每个字段的值。 完整示例代码如下: ```java import org.apache.spark.sql.*; public class JavaSparkSQLExample { public static void main(String[] args) { SparkSession sparkSession = SparkSession.builder() .appName("JavaSparkSQLExample") .config("spark.some.config.option", "some-value") .getOrCreate(); Dataset<Row> df = sparkSession.read().json("path/to/json"); df.createOrReplaceTempView("people"); Dataset<Row> sqlDF = sparkSession.sql("SELECT * FROM people"); Row[] rows = (Row[])sqlDF.collect(); for (Row row : rows) { System.out.println(row.getString(0) + ", " + row.getLong(1)); } sparkSession.stop(); } } ``` 其中,假设JSON文件中的数据格式为: ```json {"name":"Alice","age":25} {"name":"Bob","age":30} ``` 运行以上代码将输出: ``` Alice, 25 Bob, 30 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值