Parametrized ADO Stored Procedure from C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ajaxx
    New Member
    • Jan 2010
    • 4

    Parametrized ADO Stored Procedure from C++

    I posted this in the C++ section but didn’t get any responses so I thought it might be more appropriate here.

    I’m trying to call a stored procedure on my SQL 2008 server with the below code. When I look at the query with the SQL profiler I get the following:
    Code:
    exec GetUserID @Email='test@email.com',@Password='@Email'
    What I expect to get is:
    Code:
    exec GetUserID @Email='test@email.com',@Password='123456'
    It seems as if the first parameter is stepping on the second. As ‘@Email’ is a remnant of the first. Can someone explain what is going on?
    Code:
    #include "stdafx.h"
      
    #import "C:\Program Files\Common Files\System\ado\msado15.dll" rename("EOF", "EndOfFile")
     
    int _tmain(int argc, _TCHAR* argv[])
    {
        try
        {
            ::CoInitialize(NULL);
     
            ADODB::_ConnectionPtr ConnectionPtr = NULL;
            ADODB::_CommandPtr CommandPtr = NULL;
            ADODB::_ParameterPtr ParameterPtr = NULL;
     
            char ConnectionString[200] = "Provider=SQLNCLI10; Server=IL47WS030501; Database=Demo; Trusted_Connection=yes;";
     
            ConnectionPtr.CreateInstance(__uuidof(ADODB::Connection));
     
            if(ConnectionPtr)
            {
                ConnectionPtr->Open(ConnectionString, "", "", 0);
            }
     
            CommandPtr.CreateInstance(__uuidof(ADODB::Command));
     
            if(ConnectionPtr && CommandPtr)
            {
                CommandPtr->ActiveConnection = ConnectionPtr;
                CommandPtr->CommandType = ADODB::adCmdStoredProc;
                CommandPtr->CommandText = _bstr_t("GetUserID");
                CommandPtr->NamedParameters = true;
     
                VARIANT vEmail;
                vEmail.vt = VT_BSTR;
                vEmail.bstrVal = _bstr_t("test@email.com");
     
                VARIANT vPassword;
                vPassword.vt = VT_BSTR;
                vPassword.bstrVal = _bstr_t("123456");
     
                CommandPtr->Parameters->Append(CommandPtr->CreateParameter(_bstr_t("@Email"), ADODB::adVarChar, ADODB::adParamInput, sizeof(vEmail), vEmail));
                CommandPtr->Parameters->Append(CommandPtr->CreateParameter(_bstr_t("@Password"), ADODB::adVarChar, ADODB::adParamInput, sizeof(vPassword), vPassword));
     
                ADODB::_RecordsetPtr RecordsetPtr = CommandPtr->Execute(NULL, NULL, ADODB::adCmdStoredProc);
            }
     
            return 0;
        }
        catch(...)
        {
        }
    }
  • Ajaxx
    New Member
    • Jan 2010
    • 4

    #2
    I don’t know the details of why but I was able to get it working using the following code without a VARIANT instead:
    Code:
    CommandPtr->Parameters->Append(CommandPtr->CreateParameter(_bstr_t("@Email"), ADODB::adVarChar, ADODB::adParamInput, 50, "test@email.com"));
    CommandPtr->Parameters->Append(CommandPtr->CreateParameter(_bstr_t("@Password"), ADODB::adVarChar, ADODB::adParamInput, 50, "123456"));

    Comment

    Working...