Creating a Simple Consumer
Use the ATL Project Wizard and ATL OLE DB Consumer Wizard to generate an OLE DB Templates consumer.
To create a console application for an OLE DB consumer
On the File menu, click New, and then click Project.
The New Project dialog box appears.
In the Project Types pane, click the Visual C++ Projects folder, and then click the Win32 Project icon in the Templates pane. In the Name box, enter the name of your project, for example, MyCons.
Click OK.
The Win32 Project Wizard appears.
On the Application Settings page, select Console application, and then select Add support for ATL.
Click Finish to close the wizard and generate the project.
Next, use the ATL OLE DB Consumer Wizard to add an OLE DB consumer object.
To create a consumer with the ATL OLE DB Consumer Wizard
In Class View, right-click the MyCons project.
On the shortcut menu, click Add, and then click Add Class.
The Add Class dialog box appears.
In the Categories pane, click Visual C++, click the ATL OLE DB Consumer icon in the Templates pane, and then click Open.
The ATL OLE DB Consumer Wizard appears.
Click the Data Source button.
The Data Link Properties dialog box appears.
In the Data Link Properties dialog box, do the following:
On the Provider tab, specify an OLE DB provider.
On the Connection tab, specify the server name, logon ID, and password for your data source and database on the server.
Note
There is a security issue with the Allow saving of password feature of the Data Link Properties dialog box. In Enter information to log on to the server, there are two radio buttons: Use Windows NT integrated security and Use a specific user name and password.
Note
If you select Use a specific user name and password, you have the option of saving the password (using the Allow saving password check box); however, this option is not secure. It is recommended that you select Use Windows NT integrated security; this option uses Windows NT to verify your identity.
Note
If you cannot use Windows NT integrated security, you should use a middle-tier application to prompt the user for the password or to store the password in a location with security mechanisms to help protect it (instead of in source code).
After selecting your provider and other settings, click Test Connection to verify the selections made on the previous dialog box pages. If the Results box reports Test connection succeeded, click OK to create the data link.
The Select Database Object dialog box appears.
Use the tree control to select a table, view, or stored procedure. For the purpose of this procedure, select the Products table from the Northwind database.
Click OK. This returns you to the ATL OLE DB Consumer Wizard.
The wizard completes the names for Class and .h file based on the name of the table, view, or stored procedure that you selected. You can edit these names if you want.
Clear the Attributed check box so that the wizard creates the consumer code using OLE DB Template classes instead of the default OLE DB consumer attributes.
Under Type, select Command.
The wizard creates a CCommand-based consumer if you select Command or a CTable-based consumer if you select Table. The table or command class is named after the selected object, but you can edit the name.
Under Support, leave the Change, Insert, and Delete boxes cleared.
Select the Change, Insert, and Delete check boxes to support the changing, inserting, and deleting of records in the rowset, if required. For more information about writing data to the data store, see Updating Rowsets.
Click Finish to create the consumer.
The wizard generates a command class and a user record class, as shown in Consumer Wizard-Generated Classes. The command class will have the name that you entered in the Class box in the wizard (in this case, CProducts), and the user record class will have a name of the form "ClassNameAccessor" (in this case, CProductsAccessor).
Note
The wizard puts the following line into Products.h:
#error Security Issue: The connection string may contain a password
Note
This line prevents the consumer application from compiling and reminds you to check your connection string for hard-coded passwords. After checking your connection string, you can remove this line of code.