从ADODB查询返回日期(不是字符串)

时间:2013-04-10 13:39:36

标签: excel-vba adodb vba excel

下面的函数工作正常并输出正确的结果,除了它输出日期为字符串而不是日期。我怎样才能让它输出日期呢?

Function GetExpiries_YieldX(TradeDate As Date, Code As String) As Variant

    'Create and open the connection
    Dim oConnection As Connection
    Set oConnection = New Connection
    oConnection.ConnectionString = strConnectionStringYieldX
    oConnection.Open

    'Create the command object
    Dim oCommand As Command
    Set oCommand = New Command
    oCommand.CommandType = adCmdText

    Dim SQLString As String

        SQLString = "SELECT DISTINCT Expiry" _
                 & " FROM MTM" _
                 & " WHERE TradeDate = ?" _
                 & "   and Code = ?"

    oCommand.CommandText = SQLString
    oCommand.ActiveConnection = oConnection

    oCommand.Parameters.Append oCommand.CreateParameter("Date", adDBTimeStamp, adParamInput)
    oCommand.Parameters.Append oCommand.CreateParameter("Code", adVarChar, adParamInput, 50)

    oCommand.Parameters.Item("Date").Value = TradeDate
    oCommand.Parameters.Item("Code").Value = Code

    Dim result As New ADODB.Recordset
    Set result = oCommand.Execute

    Dim resultA As Variant
    'GetExpiries_YieldX = WorksheetFunction.Transpose(result.GetRows)
    GetExpiries_YieldX = result.GetRows

    oConnection.Close

End Function

1 个答案:

答案 0 :(得分:3)

2.8之前的ADODB版本已经认识到SQL Server 2008中引入了新的 Date 数据类型。

因此,请检查以下内容:

  1. 确保您在Connnection字符串中使用本机 SQL Server提供程序( NOT OLEDB SQL提供程序),因此它应该类似于“Provider = SQLNCLI10 .1;数据源= MyServer;初始目录= MyDatabase; Uid = MyUser; Pwd = MyPassword;“
  2. 确保您已安装最新的SQL Native Client(sqlncli2008.msi或类似内容,您可以从SQL Server 2008可再发行版中窃取它)
  3. 确保VB项目中引用的ADO版本为2.8
  4. ADODB & SQL 2008 (MS Forums)

    上找到了所有内容

    我希望这可以帮助您从数组中的Date列中获取数组中的Date列。