Issuing a Parameterized Query
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Issuing a Parameterized Query

This example issues a simple parameterized query that retrieves records with an age field (that is greater than 30) from a table in a Microsoft Access database. To support the parameter, the user record must have an additional map. The following code, in an ATL project, uses the CCommand class instead of the CTable class used in the previous example, Traversing a Simple Rowset.

#include <atldbcli.h>

CDataSource connection;
CSession session;
CCommand<CAccessor<CArtists> > artists;

// Open the connection, session, and table, specifying authentication 
// using Windows NT integrated security. Hard-coding a password is a major 
// security weakness.
connection.Open(CLSID_MSDASQL, "NWind", NULL, NULL, 

// Set the parameter for the query
artists.m_nAge = 30;
artists.Open(session, "select * from artists where age > ?");

// Get data from the rowset
while (artists.MoveNext() == S_OK)
   cout << artists.m_szFirstName;
   cout << artists.m_szLastName;

The user record, CArtists, looks like this:

class CArtists
// Data Elements
   CHAR m_szFirstName[20];
   CHAR m_szLastName[30];
   short m_nAge;
// Column binding map
   COLUMN_ENTRY(1, m_szFirstName)
   COLUMN_ENTRY(2, m_szLastName)
   COLUMN_ENTRY(3, m_nAge)
// Parameter binding map
   COLUMN_ENTRY(1, m_nAge)

See Also

Working with OLE DB Consumer Templates

© 2015 Microsoft