By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,806 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,806 IT Pros & Developers. It's quick & easy.

Parametrized ADO Stored Procedure from C++

P: 4
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:
Expand|Select|Wrap|Line Numbers
  1. exec GetUserID @Email='test@email.com',@Password='@Email'
What I expect to get is:
Expand|Select|Wrap|Line Numbers
  1. 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?
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2.  
  3. #import "C:\Program Files\Common Files\System\ado\msado15.dll" rename("EOF", "EndOfFile")
  4.  
  5. int _tmain(int argc, _TCHAR* argv[])
  6. {
  7.     try
  8.     {
  9.         ::CoInitialize(NULL);
  10.  
  11.         ADODB::_ConnectionPtr ConnectionPtr = NULL;
  12.         ADODB::_CommandPtr CommandPtr = NULL;
  13.         ADODB::_ParameterPtr ParameterPtr = NULL;
  14.  
  15.         char ConnectionString[200] = "Provider=SQLNCLI10; Server=IL47WS030501; Database=Demo; Trusted_Connection=yes;";
  16.  
  17.         ConnectionPtr.CreateInstance(__uuidof(ADODB::Connection));
  18.  
  19.         if(ConnectionPtr)
  20.         {
  21.             ConnectionPtr->Open(ConnectionString, "", "", 0);
  22.         }
  23.  
  24.         CommandPtr.CreateInstance(__uuidof(ADODB::Command));
  25.  
  26.         if(ConnectionPtr && CommandPtr)
  27.         {
  28.             CommandPtr->ActiveConnection = ConnectionPtr;
  29.             CommandPtr->CommandType = ADODB::adCmdStoredProc;
  30.             CommandPtr->CommandText = _bstr_t("GetUserID");
  31.             CommandPtr->NamedParameters = true;
  32.  
  33.             VARIANT vEmail;
  34.             vEmail.vt = VT_BSTR;
  35.             vEmail.bstrVal = _bstr_t("test@email.com");
  36.  
  37.             VARIANT vPassword;
  38.             vPassword.vt = VT_BSTR;
  39.             vPassword.bstrVal = _bstr_t("123456");
  40.  
  41.             CommandPtr->Parameters->Append(CommandPtr->CreateParameter(_bstr_t("@Email"), ADODB::adVarChar, ADODB::adParamInput, sizeof(vEmail), vEmail));
  42.             CommandPtr->Parameters->Append(CommandPtr->CreateParameter(_bstr_t("@Password"), ADODB::adVarChar, ADODB::adParamInput, sizeof(vPassword), vPassword));
  43.  
  44.             ADODB::_RecordsetPtr RecordsetPtr = CommandPtr->Execute(NULL, NULL, ADODB::adCmdStoredProc);
  45.         }
  46.  
  47.         return 0;
  48.     }
  49.     catch(...)
  50.     {
  51.     }
  52. }
Jan 9 '10 #1
Share this Question
Share on Google+
1 Reply


P: 4
I don’t know the details of why but I was able to get it working using the following code without a VARIANT instead:
Expand|Select|Wrap|Line Numbers
  1. CommandPtr->Parameters->Append(CommandPtr->CreateParameter(_bstr_t("@Email"), ADODB::adVarChar, ADODB::adParamInput, 50, "test@email.com"));
  2. CommandPtr->Parameters->Append(CommandPtr->CreateParameter(_bstr_t("@Password"), ADODB::adVarChar, ADODB::adParamInput, 50, "123456"));
Jan 10 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.