如何在阵列中获取MS-Access数据?
我试过了:
Dim i As Integer = 0
Dim farmers() As String = {}
sql = "SELECT * FROM users"
cmd = New OleDbCommand(sql, connection)
reader = cmd.ExecuteReader()
If Not reader.HasRows Then
'nothing
ElseIf reader.HasRows Then
Do While reader.Read
farmers(i) = CStr(reader.Item(1))
i += 1
Loop
End If
For Each element As String In farmers
MsgBox(element)
Next
显示错误:
我的MS-Access
数据库表是:
users
____________________________
| ID | Number | Name |
| 1 | 10 | John |
| 2 | 15 | Joe |
| 3 | 7 | User3 |
我想在数组中使用10 15 7
。
我怎么能得到它?还是其他任何方法?
感谢任何帮助。谢谢
答案 0 :(得分:1)
您的数组没有维度,您必须重新调整数组以包含查询返回的所有元素 类似的东西:
Dim i As Integer = 0
Dim farmers() As String = {}
sql = "SELECT * FROM users"
cmd = New OleDbCommand(sql, connection)
reader = cmd.ExecuteReader()
If Not reader.HasRows Then
'nothing
ElseIf reader.HasRows Then
'-->> Add this line
ReDim(farmers,reader.rows.count)
Do While reader.Read
farmers(i) = CStr(reader.Item(1))
i += 1
Loop
End If
For Each element As String In farmers
MsgBox(element)
Next