Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba Azure检索空白记录集_Vba_Excel_Azure - Fatal编程技术网

Vba Azure检索空白记录集

Vba Azure检索空白记录集,vba,excel,azure,Vba,Excel,Azure,我当前正在尝试从Azure托管的数据库检索Excel中的记录集 它检索正确的记录计数,但将它们作为选项卡(而不是正确的数据)写出。我可以在查询数据库时确认代码中调用的查询正在检索记录 代码如下: Dim cnt As New ADODB.Connection Dim rst As New ADODB.Recordset Dim xlApp As Object Dim iLastCellReference As Integer Dim recArray As Variant Dim strDB A

我当前正在尝试从Azure托管的数据库检索Excel中的记录集

它检索正确的记录计数,但将它们作为选项卡(而不是正确的数据)写出。我可以在查询数据库时确认代码中调用的查询正在检索记录

代码如下:

Dim cnt As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim xlApp As Object
Dim iLastCellReference As Integer
Dim recArray As Variant
Dim strDB As String
Dim fldCount As Integer
Dim recCount As Long
Dim iCol As Integer
Dim iRow As Integer
Dim sRange As String

' Set the string to the path of your database
strDB = Connection.Range("A2").Value
On Error GoTo ErrorHandler
' Open connection to the database
cnt.Open strDB


' Open recordset based on Project_Information table
rst.Open "Select DISTINCT Project_Current_Name From psruser.Project_Master_Table where Project_Enabled = 'True'", cnt
'rst.Open "Select DISTINCT Project_Number From mruser.Project_Information", cnt

If Not (rst.BOF And rst.EOF) Then

' Create an instance of Excel and add a workbook
Set xlApp = CreateObject("Excel.Application")

' Copy field names to the first row of the worksheet
fldCount = rst.Fields.Count

For iCol = 1 To fldCount
Parameters.Cells(82, iCol).Value = rst.Fields(iCol - 1).Name
Next

' Below is to get the number of rows
' Copy recordset to an array
recArray = rst.GetRows

'Note: GetRows returns a 0-based array where the first
'dimension contains fields and the second dimension
'contains records. We will transpose this array so that
'the first dimension contains records, allowing the
'data to appears properly when copied to Excel

' Determine number of records
recCount = UBound(recArray, 2) + 1 '+ 1 since 0-based array

' Copy the recordset to the worksheet, starting in cell A132
' Check the array for contents that are not valid when
' copying the array to an Excel worksheet
For iCol = 0 To fldCount - 1
For iRow = 0 To recCount - 1
    ' Take care of Date fields
    If IsDate(recArray(iCol, iRow)) Then
        recArray(iCol, iRow) = Format(recArray(iCol, iRow))
    ' Take care of OLE object fields or array fields
    Else
        If IsArray(recArray(iCol, iRow)) Then
            recArray(iCol, iRow) = "Array Field"
        End If
    End If
'Next iRow 'next record
Next iRow
'Next iCol 'next field
Next iCol

' Transpose and Copy the array to the worksheet, starting in cell A132
Parameters.Cells(82, 1).Resize(recCount, fldCount).Value = _
TransposeDim(recArray)

' Close ADO objects
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing

原来是连接字符串,Windows Azure生成的字符串最多只能是片状的,它允许插入和更新记录,并执行存储过程,但不允许解析查询


我强烈建议您编写自己的连接字符串,不要使用Azure连接字符串

打开记录集后,如果您仅使用
CopyFromRecordset
,是否看到任何记录?