Traversing a Simple Rowset

The following example shows a quick and easy database access that does not involve commands. The code retrieves records from a table called Artists in a Microsoft Access database using the Microsoft ODBC OLE DB Provider. The code creates an instance of CTable with CAccessor<CArtists> as a template parameter. It opens a connection, opens a session on the connection, and opens the table on the session.

#include <atldbcli.h>

CDataSource connection;
CSession session;
CTable<CAccessor<CArtists> > artists;
 
// Open the connection, session, and table
connection.Open(CLSID_MSDASQL, "NWind", "sa", "");
session.Open(connection);
artists.Open(session, "Artists");
 
// 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
{
public:
// Data Elements
   CHAR m_szFirstName[20];
   CHAR m_szLastName[30];
   short m_nAge;
 
// output binding map
BEGIN_COLUMN_MAP(CArtists)
   COLUMN_ENTRY(1, m_szFirstName)
   COLUMN_ENTRY(2, m_szLastName)
   COLUMN_ENTRY(3, m_nAge)
END_COLUMN_MAP()

Back to Common OLE DB Consumer Scenarios (Examples)