ADO 记录集不使用 DSN 返回字段值;使用连接字符串
ADO Recordset Not Returning Field Value With DSN; Does with Connection String
我有以下功能:
Function downloadsqltoexcel(conn As ADODB.Connection, sSQL As String, exceldestinationrangename As String, sqltablename As String, bDownload As Boolean, Optional ws As Worksheet) As Variant
'================================================================================================================================
'== Procedure Name: downloadsqltoexcel
'== Purpose: downloads SQL table data (or query data) to Excel named range or grabs a specific value from an SQL table
'== Notes: ensure that SQL table or query name and Excel named range are identical
'================================================================================================================================
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset
Dim DestinationRange As Range
Dim sqlstring As String
Dim s As String
With rsPubs
.ActiveConnection = conn
.Open sSQL, conn, adOpenStatic, adLockReadOnly, adCmdText
If bDownload Then 'if download switch is on, dump into Excel named range
If ws Is Nothing Then
Set DestinationRange = Range(exceldestinationrangename)
Else
Set DestinationRange = ws.Range(exceldestinationrangename)
End If
With DestinationRange
.ClearContents
.CopyFromRecordset rsPubs
End With
'.... more code below that is not relevant
假设我这样调用函数:
Dim conPDDB As New ADODB.Connection
conPDDB.Open "myConnectionString" - see below
Dim sSQL as String
sSQL = "SELECT ... "
downloadsqltoexcel conPDDB, sSQL, "DSN", "", True, Sheet1
如果我将硬编码连接字符串传递到 myConnectionString
,我会得到我想要的结果。如果我传递 DSN 连接字符串,最后一列数据不会转储到 Excel.
见图
Provider=SQLOLEDB;Data Source=MYSERVER;Initial Catalog=MYDB;User
Id=MYUID;Password=MYPWD
DSN=PDDB_QA;UID=MYUID;PWD=MYPWD (DSN configmirrors connection string)
字段本身是 return,因为当我将 ?rsPubs.Fields(5).Value
打印到立即数 window 时,我在 return 中收到 AU,CI,GL,IM,PF
。本例中最后的数据有逗号,但并不总是逗号。我试过使用 DSN 设置和 rs.Open
参数。徒劳无功。
有人有什么想法吗?
我暂时将此作为答案,但我对解决方案不满意。 (我现在将实施,因为距离发布还有两周时间,而且还有 大量 的工作要做)。
如果我没有找到,或者没有人提供更好的解决方案,我会接受这个并继续前进。
将 .CopyFromRecordset rsPubs
行重构到下面,带来最后一列。
Dim r as Integer
r = 1
With DestinationRange
.ClearContents
'.CopyFromRecordset rsPubs
rsPubs.MoveFirst
Do Until rsPubs.EOF
Dim c As Integer
For c = 0 To rsPubs.Fields.Count - 1
DestinationRange.Cells(r, c + 1).Value = rsPubs.Fields(c)
Next
rsPubs.MoveNext
r = r + 1
Loop
End With
我有以下功能:
Function downloadsqltoexcel(conn As ADODB.Connection, sSQL As String, exceldestinationrangename As String, sqltablename As String, bDownload As Boolean, Optional ws As Worksheet) As Variant
'================================================================================================================================
'== Procedure Name: downloadsqltoexcel
'== Purpose: downloads SQL table data (or query data) to Excel named range or grabs a specific value from an SQL table
'== Notes: ensure that SQL table or query name and Excel named range are identical
'================================================================================================================================
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset
Dim DestinationRange As Range
Dim sqlstring As String
Dim s As String
With rsPubs
.ActiveConnection = conn
.Open sSQL, conn, adOpenStatic, adLockReadOnly, adCmdText
If bDownload Then 'if download switch is on, dump into Excel named range
If ws Is Nothing Then
Set DestinationRange = Range(exceldestinationrangename)
Else
Set DestinationRange = ws.Range(exceldestinationrangename)
End If
With DestinationRange
.ClearContents
.CopyFromRecordset rsPubs
End With
'.... more code below that is not relevant
假设我这样调用函数:
Dim conPDDB As New ADODB.Connection
conPDDB.Open "myConnectionString" - see below
Dim sSQL as String
sSQL = "SELECT ... "
downloadsqltoexcel conPDDB, sSQL, "DSN", "", True, Sheet1
如果我将硬编码连接字符串传递到 myConnectionString
,我会得到我想要的结果。如果我传递 DSN 连接字符串,最后一列数据不会转储到 Excel.
见图
Provider=SQLOLEDB;Data Source=MYSERVER;Initial Catalog=MYDB;User Id=MYUID;Password=MYPWD
DSN=PDDB_QA;UID=MYUID;PWD=MYPWD (DSN configmirrors connection string)
字段本身是 return,因为当我将 ?rsPubs.Fields(5).Value
打印到立即数 window 时,我在 return 中收到 AU,CI,GL,IM,PF
。本例中最后的数据有逗号,但并不总是逗号。我试过使用 DSN 设置和 rs.Open
参数。徒劳无功。
有人有什么想法吗?
我暂时将此作为答案,但我对解决方案不满意。 (我现在将实施,因为距离发布还有两周时间,而且还有 大量 的工作要做)。
如果我没有找到,或者没有人提供更好的解决方案,我会接受这个并继续前进。
将 .CopyFromRecordset rsPubs
行重构到下面,带来最后一列。
Dim r as Integer
r = 1
With DestinationRange
.ClearContents
'.CopyFromRecordset rsPubs
rsPubs.MoveFirst
Do Until rsPubs.EOF
Dim c As Integer
For c = 0 To rsPubs.Fields.Count - 1
DestinationRange.Cells(r, c + 1).Value = rsPubs.Fields(c)
Next
rsPubs.MoveNext
r = r + 1
Loop
End With