2014-05-13 45 views
0

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

它检索正确的记录数,但将它们写成标签 - 而不是正确的数据。我可以确认在代码中调用的查询是在查询数据库时检索记录。下面

代码:

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 
+0

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

回答

0

原来这是在连接字符串,在Windows Azure生成的字符串是古怪充其量,这将允许插入和更新记录,并执行存储过程而不是一个查询通过解析。

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