MDAC 2.5 SDK - OLE DB Providers
OLE DB Persistence Provider


 

Invoking the Persistence Provider

See Also    Interfaces               

The OLE DB Persistence Provider is automatically invoked when the ADO Save method is called on the IADORecordset interface, with a pointer to the recordset and with the local file name specified. The following example in Microsoft® Visual C++® shows the SaveMyRowset method for rowset persistence. The example follows these steps:

  1. An empty recordset is constructed.

  2. The rowset is given to ADO, where the rowset is merged into the constructed recordset.

  3. The recordset's Save method (defined in the following code) is invoked, passing in as arguments the file name (bstrFileName) and the desired persisted file format (adPersist*), where * is either "XML" or "ADTG".

For more examples, see the "Microsoft OLE DB Persistence Provider" documentation in the ADO Programmer's Reference.

HRESULT SaveMyRowset(IRowset *pRowset, BSTR bstrFileName)
{
   // Get the recordset interface
   IADORecordset * pRs = NULL;
   IADORecordsetConstruction * pADOsCt = NULL;
   HRESULT hr;

// Get an ADO recordset, and ask for the recordset construction interface

   if (FAILED (hr = ::CoCreateInstance(
               CLSID_ADORecordset,
               NULL,
               CLSCTX_INPROC,
               IID_IADORecordsetConstruction,
               (LPVOID *) &pADORsCt)))
      return hr;

// Give the rowset to ADO

   if (SUCCEEDED (hr= pADOsCt->put_Rowset(pRowset))
   {
      hr = pADORsCt->QueryInterface(IID_IADORecordset, (void **)&pRs);
   // Identify which format to use when persisting: XML or ADTG
      hr = pRs->Save(bstrFileName, adPersistADTG;
}
// Cleanup (or use the ATL CComPtr class):

pRs->Release();
pADORsCt->Release();

return hr;
}

Note   The result of saving hierarchical rowsets is different because they are persisted as separate, flattened rowsets. For more information about persisting hierarchical rowsets, see the "Persisted Hierarchical Data" section of this guide.

Loading a persisted rowset into a recordset so that it can be viewed or modified by the application requires the persisted file name and a pointer to the IRowset interface. The following Visual C++ example shows the LoadMyFile method for loading the persisted data into a recordset. The example follows these steps:

  1. An empty recordset is constructed.

  2. The persisted file is opened (as identified by bstrFileName).

  3. The rowset is loaded into the recordset.

For more examples, see the "Microsoft OLE DB Persistence Provider" documentation in the ADO Programmer's Reference.

HRESULT LoadMyFile(BSTR bstrFileName, IRowset ** ppRs)
{
   // Get the recordset interface

   IADORecordsetConstruction * pRsCt = NULL;
   IADORecordset * pADORs = NULL;
   HRESULT hr;

   // Create the ADO Recordset

   if (SUCCEEDED (hr = ::CoCreateInstance(CLSID_ADORecordset,
                  NULL,
                  CLSCTX_INPROC,
                  IID_IADORecordset,
                  (LPVOID *) &pADORs)))
{
   VARIANNT varEmpty;

   varEmpty.vt = VT_ERROR;
   varEmpty.scode = DISP_E_PARAMNOTFOUND;

   // Open the persisted file 

   hr = pADORs->Open(bstrFileName, varEmpty, -1,-1,-1);
   hr = pADORs->QueryInterface(IID_IADORecordsetConstruction,
   (void **)&pRsCt);

   // Loads the persisted rowset into the Recordset

   hr = pRsCt->get_Rowset (ppRs);
}
   pADOrs->Release() ;
   pRsCt->Release() ;
   return hr;
}

Note   The procedures for saving a rowset do not apply to hierarchical rowsets because they are persisted as separate, flattened rowsets. For more information about persisting hierarchical rowsets, see the "Persisted Hierarchical Data" section of this guide.