Parameter Object, Parameters Collection Example (VC++)

This example sets two query parameters for a hypothetical parameter query named “ParamQuery,” executes the query by opening a Recordset from the QueryDef, and then prints the properties of each parameter. Note that this query does not actually exist in the Northwind sample database. The parameter data types are of type Date. See the methods and properties listed in the Parameter summary topic for additional examples.

CdbDBEngine         dbeng;
CdbDatabase         dbsCurrent;
CdbQueryDef         qdfParam;
CdbParameter      prmEnum;
CdbRecordset      rstParam;
long            X;

dbsCurrent = dbeng.OpenDatabase(_T("Northwind.mdb"));
qdfParam = dbsCurrent.QueryDefs[_T("ParamQuery")];

// Set parameters.
qdfParam.Parameters[_T("Order Date")].SetValue(COleVariant(_T("10/11/94")));
qdfParam.Parameters[_T("Ship Date")].SetValue(COleVariant(_T("11/4/94")));

rstParam = qdfParam.OpenRecordset();

// Show parameter properties.
for (X = 0; X < qdfParam.Parameters.GetCount(); X++)
   {
   prmEnum = qdfParam.Parameters[X];
   printf("%s %d\n", prmEnum.GetName(),   // Print parameter properties.
                  prmEnum.GetType());
   }

rstParam.Close;