Azure检索空白记录集

时间:2014-05-13 04:57:48

标签: excel vba excel-vba 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 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

1 个答案:

答案 0 :(得分:0)

原来它是连接字符串,Windows Azure生成的字符串充其量只是flakey,它允许插入和更新记录,执行存储过程但不解析查询。

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