如何将SQL查询结果存放到数组里面
展开全部
Private Sub Command1_Click()
Dim conn As ADODB.Connection
Dim Rs As ADODB.Recordset
Set conn = CreateObject("ADODB.Connection")
Set Rs = CreateObject("ADODB.Recordset")
conn.Open "provider=microsoft.jet.oledb.4.0;data source=d:\db1.mdb"
Rs.Open "select id from table1", conn, 1, 3
''''''''''''''以上不是重点,只是为了链接数据库
Dim a() As Long'定义a数组
Dim l As Long
l = 0
While Not Rs.EOF
ReDim Preserve a(0 To l) As Long '重定义数组大小
a(l) = Rs("id") '保存新数据
l = l + 1
Rs.MoveNext
Wend
'''''''''''''''''''以下也不是重点
Rs.Close
conn.Close
Set conn = Nothing
Set Rs = Nothing
For l = 0 To UBound(a)
Print a(l);
Next l
End Sub
Dim conn As ADODB.Connection
Dim Rs As ADODB.Recordset
Set conn = CreateObject("ADODB.Connection")
Set Rs = CreateObject("ADODB.Recordset")
conn.Open "provider=microsoft.jet.oledb.4.0;data source=d:\db1.mdb"
Rs.Open "select id from table1", conn, 1, 3
''''''''''''''以上不是重点,只是为了链接数据库
Dim a() As Long'定义a数组
Dim l As Long
l = 0
While Not Rs.EOF
ReDim Preserve a(0 To l) As Long '重定义数组大小
a(l) = Rs("id") '保存新数据
l = l + 1
Rs.MoveNext
Wend
'''''''''''''''''''以下也不是重点
Rs.Close
conn.Close
Set conn = Nothing
Set Rs = Nothing
For l = 0 To UBound(a)
Print a(l);
Next l
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询