JavaScript EditorDhtml editor     Free javascript download 



Main Page

The following example assumes that you are adding OLE DB consumer support to an existing ATL project. If you want to add OLE DB consumer support to an MFC application, you should run the MFC Application Wizard, which creates all the support necessary and invokes MFC routines necessary to execute the application.

To add OLE DB consumer support without using the ATL OLE DB Consumer Wizard:

  • In your Stdafx.h file, append the following #include statements:

    В CopyCode imageCopy Code
    #include <atlbase.h>
    #include <atldbcli.h>
    #include <atldbsch.h> // if you are using schema templates

Programmatically, a consumer typically performs the following sequence of operations:

  • Create a user record class that binds columns to local variables. In this example, CMyTableNameAccessor is the user record class (see User Records). This class contains the column map and parameter map. Declare a data member in the user record class for each field you specify in your column map; for each of these data members, also declare a status data member and a length data member. For more information, see Field Status Data Members in Wizard-Generated Accessors.

    NoteNote

    If you write your own consumer, the data variables must come before the status and length variables.

  • Instantiate a data source and a session. Decide what type of accessor and rowset to use and then instantiate a rowset using CCommand or CTable:

    В CopyCode imageCopy Code
    CDataSource ds;
    CSession ss;
    class CMyTableName : public CCommand<CAccessor<CMyTableNameAccessor> >
  • Call CoInitialize to initialize COM. This is usually called in the main code. For example:

    В CopyCode imageCopy Code
    HRESULT hr = CoInitialize(NULL);
  • Call CDataSource::Open or one of its variations.

  • Open a connection to the data source, open the session, and open and initialize the rowset (and if a command, also execute it):

    В CopyCode imageCopy Code
    hr = ds.Open();
    hr = ss.Open(ds);
    hr = rs.Open();            // (Open also executes the command)
  • Optionally, set rowset properties using CDBPropSet::AddProperty and pass them as a parameter to rs.Open. For an example of how this is done, see GetRowsetProperties in Consumer Wizard-Generated Methods.

  • You can now use the rowset to retrieve/manipulate the data.

  • When your application is done, close the connection, session, and rowset:

    В CopyCode imageCopy Code
    rs.Close();
    ss.Close();
    ds.Close();

    If you are using a command, you might want to call ReleaseCommand after Close. The code example in CCommand::Close shows how to call Close and ReleaseCommand.

  • Call CoUnInitialize to uninitialize COM. This is usually called in the main code.

    В CopyCode imageCopy Code
    CoUninitialize();

Expand imageSee Also



JavaScript EditorDhtml editor     Free javascript download