How to get the executed SQL test from an adodb command with parameters

  adodb, c++, sql

If I have a C++ ADODB Command with parameters setup like this:

// Create the command
std::string query = "DELETE from x where id = ?";
m_pCommand.CreateInstance(__uuidof(ADODB::Command));
m_pCommand->CommandType = ADODB::adCmdText;
m_pCommand->CommandText = _bstr_t(query.c_str());

// Create the parameter    
ADODB::_ParameterPtr parameter;
parameter.CreateInstance(__uuidof(ADODB::Parameter));
parameter->PutDirection(ADODB::ParameterDirectionEnum::adParamInput);
parameter->PutType(ADODB::DataTypeEnum::adBigInt);
parameter->PutSize(sizeof(long));
parameter->PutValue(1234L);

// Add the parameter
m_pCommand->Parameters->Append(parameter);

// Execute the command
_variant_t vtRecordsAffected;
pCommand->ActiveConnection = m_pConnection;
pCommand->Execute(&vtRecordsAffected, NULL, ADODB::adCmdText);

Is it possible to get the actual SQL that was executed on the server?

According to the docs

Depending on the CommandType property setting, ADO may alter the CommandText property. You can read the CommandText property at any time to see the actual command text that ADO will use during execution.

But I do not see the commandText property being updated

https://docs.microsoft.com/en-us/sql/ado/reference/ado-api/commandtext-property-ado?view=sql-server-ver15

Source: Windows Questions C++

LEAVE A COMMENT