AdvancedPV Sample: Demonstrates Advanced Provider Techniques
The AdvancedPV sample is very similar to UpdatePV, but it demonstrates some advanced techniques.
Normally providers written using OLE DB Templates use CAtlArray for data storage. The OLE DB Provider Templates call a user-provided Execute method to populate the array (for example, to load all of the rows from the data file into the array). Another user-provided method, FlushData, is used to save the contents of the array (for example, to write the array contents back into the data file).The problem with this approach is that in Execute you have to load all of the rows in the rowset, and in FlushData you have to save all of the rows at the same time. If there is a large number of rows in the rowset, all of the data needs to be stored in memory (in the CAtlArray object).
AdvancedPV demonstrates how to use a special array class in place of the default CAtlArray array to make the provider load and save rows as necessary. The rows will be loaded from the data file only when they are actually requested (through a specially implemented operator[]) and the changes will be written back to the file as soon as the array contents change.
Security Note |
---|
This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties. |
To get samples and instructions for installing them:
To access samples from Visual Studio
On the Help menu, click Samples.
By default, these samples are installed in drive:\Program Files\Microsoft Visual Studio 10.0\Samples\.
- For the most recent version of this sample and a list of other samples, see Visual Studio Samples on the MSDN Web site.
Building and Running the Sample
To build and run this sample
Open the solution file AdvancedPV.sln.
From the Build menu, click Build.
Create a Win32 Console application with the Win32 Project wizard. Give it ATL support.
Add an OLE DB consumer to the project (from Add Class, select ATL OLE DB Consumer).
In the ATL OLE DB Consumer wizard, click the Data Source button and in Data Link Properties, select AdvancedProv Provider. (The AdvancedProv provider should be registered automatically when you build AdvancedPV, but if you don't see it listed here, run regsvr32.exe on AdvancedPV.dll.)
Click Next to go to the Connection tab, then under Enter the initial catalog to use, specify the initial catalog to use (the path to DataFile.dat).
Under Select Database Object, open Tables; there is only one item (the path to DataFile.dat). Select it and click OK. When you return to the ATL OLE DB Consumer wizard, select Table, rename the class to something shorter (if necessary) such as CMyCons, and click Finish. Build the consumer project.
Add the following to your project's main code:
#include "MyCons.h" int main( int argc, char* argv[] ) { // Add this code HRESULT hr = CoInitialize(NULL); CMyCons rs; hr = rs.OpenAll(); ATLASSERT( SUCCEEDED(hr)); hr = rs.MoveFirst(); while( SUCCEEDED(hr) && hr != DB_S_ENDOFROWSET ) { printf( "%d %s %s %s %s\n", rs.m_Fixed, rs.m_Command, rs.m_Text, rs.m_Command2, rs.m_Text2 ); hr = rs.MoveNext(); } rs.CloseAll(); CoUninitialize(); return 0; }
Put a breakpoint on the CoUninitialize function; this will make the console stay open so you can view the results. Run the application by clicking the Start button (or click Start Without Debugging from the Debug menu). You should see five columns of text printed out (one index and four text columns).
Keywords
This sample uses the following interfaces:
IRowsetLocateImpl, IRowsetScroll, IRowsetScrollImpl, IRowsetUpdateImpl, IConnectionPointContainerImpl, IRowsetNotifyCP, IDBCreateSessionImpl, IDBInitializeImpl, IDBPropertiesImpl, IPersistImpl, IInternalConnectionImpl, IGetDataSourceImpl, IOpenRowsetImpl, ISessionPropertiesImpl, IObjectWithSiteSessionImpl, IDBSchemaRowsetImpl, IDBCreateCommandImpl, IAccessorImpl, ICommandTextImpl, ICommandPropertiesImpl, IObjectWithSiteImpl, IConvertTypeImpl, IColumnsInfoImpl, IInternalCommandConnectionImpl
The sample demonstrates the following classes:
CSchemaRowsetImpl, CComObjectRootEx, CComObjectRootEx, CRowsetImpl, CFileArray, CSimpleRow
The sample demonstrates the following macros:
COM_INTERFACE_ENTRY, PROPERTY_INFO_ENTRY