Let’s see an example of how we can use parameters in our queries.
We will use the nation table and we will be querying table data using the code column, based on a parameter we will specify dynamically.
First, we need to define the proper data structure:
class CNationAccessorEx
{
public:
TCHAR m_Code4;
TCHAR m_Name41;
TCHAR m_Continent11;
TCHAR m_Capital31;
BEGIN_COLUMN_MAP(CNationAccessor)
COLUMN_ENTRY(1, m_Code)
COLUMN_ENTRY(2, m_Name)
COLUMN_ENTRY(3, m_Continent)
COLUMN_ENTRY(4, m_Capital)
END_COLUMN_MAP()
BEGIN_PARAM_MAP(CNationAccessor)
SET_PARAM_TYPE(DBPARAMIO_INPUT)
COLUMN_ENTRY(1, m_Code)
END_PARAM_MAP()
DEFINE_COMMAND_EX(CNationAccessorEx, L"SELECT * FROM nation where code = ?")
};
Please note that the “trick” is to use the BEGIN_PARAM_MAP.
And all we have to do now is to specify the proper parameter value and query the data, as shown below:
CCommand <CAccessor<CNationAccessorEx>> nation; //Set the parameter for the query wcscpy(nation.m_Code, L"YEM"); hr = nation.Open(session);