如何使用ADODB在一次执行中返回多个记录集?

时间:2016-01-07 18:52:36

标签: sql vba adodb

我需要迭代单个查询生成的多个recodset。

但是我当前的连接似乎不支持这样做。所以当我这样做时.NextRecordset我收到了消息:

  

当前提供程序不支持从a返回多个记录集   单次执行

这是我的连接字符串:

DB_CONNECTION = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Forecasting;Integrated Security=SSPI;"
Call Conn.Open(DB_CONNECTION)

我该怎样做才能使用.NextRecordset?

2 个答案:

答案 0 :(得分:0)

为什么不定义您想要的多个记录集:

Dim rs1 As String
Dim rs2 As String

等等。然后,您可以每次使用另一个记录集运行查询。

也许以下是可能的:

Dim Recordset As String
Recordset = Array("Select * From *", "Select * From * where....", (aso))
For i = LBound(Recordset) - UBound(Recordset)   'not sure if it's the other way around
    db.Recordset = Recordset(i)
Next i

我不确定,但也许我的语法有点不对......

答案 1 :(得分:0)

将CursorLocation设置为 adUseServer (而不是'客户端')

Set RS = New ADODB.Recordset
strSQL = "Select * from States; Select * from Countries;"

With RS
   .CursorLocation = adUseServer
   .ActiveConnection = DB_CONNECTION
   .CursorType = adOpenStatic
   .Open strSQL
End With

Do
   If Not RS.EOF Then
       'do something
   End If
   Set RS = RS.NextRecordset
   If RS Is Nothing Then
       Exit Do
   End If
Loop Until RS.State = adStateClosed