开发者

Parameterised Query to Oracle in ADO Crashes Excel

开发者 https://www.devze.com 2023-03-28 23:17 出处:网络
I have set up a parameterised query, using ADO 2.7 inside Excel 2007. The database connection is made via ODBC and every attempt at executing the query results in Excel crashing...and when I say \'cra

I have set up a parameterised query, using ADO 2.7 inside Excel 2007. The database connection is made via ODBC and every attempt at executing the query results in Excel crashing...and when I say 'crashing', I mean it has to close and restart!

At first I thought this was a problem with my query being too long (involving multiple parameters); but I tried a very basic, single parameter, query and had the same problem... This is using positional/unnamed parameters, so perha开发者_如何学运维ps that's the problem; but I'm not sure if I can use named parameters with an Oracle query unless I wrap it up in a stored function in the schema.

My code, for what it's worth:

Open the Database (DBConnection is a custom type with DSN, Username, xPassword and Key members, the password is stored in memory with rudimentary encryption; there is a corresponding dbClose function, which I omit here.)

Private Function dbOpen(ByRef db As ADODB.Connection, Creds As DBConnection) As Boolean
    On Error Resume Next

    Set db = New ADODB.Connection
    db.Open Creds.DSN, Creds.Username, XorC(Creds.xPassword, Creds.Key)

    If db.State <> adStateOpen Then dbOpen = False Else dbOpen = True
End Function

Get Data (Simplified, with one parameter, for sake of example.)

Public Function GetData(ODBC As DBConnection, myParam As String) As ADODB.Recordset
    Dim myDB As ADODB.Connection
    Dim myQuery As ADODB.Command
    Dim Parameter As ADODB.Parameter

    Set myDB = New ADODB.Connection

    If dbOpen(myDB, ODBC) Then
        Set myQuery = New ADODB.Command
        myQuery.ActiveConnection = myDB
        myQuery.CommandText = "select * from sometable where id = ?"

        Set Parameter = myQuery.CreateParameter(, adVarChar, adParamInput, 5, myParam)

        Set GetData = myQuery.Execute

        dbClose myDB
    Else
        MsgBox "Cannot connect to the database.", vbExclamation
        Set GetData = Nothing
    End If
End Function

Testing routine that will crash Excel

Public Sub Test()
    Dim Connection As DBConnection
    Dim myData As ADODB.Recordset

    With Connection
        .DSN = "myDSN"
        .Username = "dbUser"
        .Key = Now
        .xPassword = XorC("p4s5w0rd", .Key)
    End With

    Set myData = GetData(Connection, "AB123")  ' Crashes here
    dbClose myData
End Sub


You're not adding the parameter you create to the Command object:

myQuery.Parameters.Append Parameter 

Just creating it doesn't add it...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号