Microsoft KB Archive/242249

From BetaArchive Wiki

Article ID: 242249

Article Last Modified on 8/5/2004



APPLIES TO

  • Microsoft ActiveX Data Objects 1.5
  • Microsoft ActiveX Data Objects 2.0
  • Microsoft ActiveX Data Objects 2.1
  • Microsoft ActiveX Data Objects 2.5
  • Microsoft ActiveX Data Objects 2.6
  • Microsoft ActiveX Data Objects 2.7



This article was previously published under Q242249

SUMMARY

ADOPERSIST.exe is a Visual C++ sample that shows you how to save an ADO recordset to an IStream object and then reload another ADO recordset from it.

ADO 1.5x and 2.x Recordset implementation offers two functions for data persistance to a disk file:

  1. ADORecordset.Save() to save the recordset object to a file.
  2. ADORecordset.Open() to reload a recordset from a file.

For performance or other reasons it may sometimes be desired to save an ADO recordset to the memory only as a stream of data. To accomplish that you can use the ADO recordset's IPersistStream interface implementation.


MORE INFORMATION

Saving an ADO Recordset Object

This sample uses the following steps to save an ADO recordset:

  1. Calls QueryInterface() on the given ADO recordset to get a pointer to the IPersistStream object.
  2. Uses CreateStreamOnHGlobal() to create a standard, COM, IStream object.
  3. Calls the COM function OleSaveToStream() to save the IPersistStream object into the IStream object.

Recreating the ADO Recordset

Given the IStream object, the sample then recreates another ADO recordset from it. It does this using the IStream pointer that holds the data stream and calls OleLoadFromStream().

Below is the key code from the sample:

int main(int argc, char* argv[])
{
    InitOle oleinit;

    HRESULT hr=S_OK;
    _RecordsetPtr rs; //Recordset for Saving Data
    _RecordsetPtr rs2; //Recordset for Loading Data
    IStreamPtr pStream;

//----------------------------------------
//  Create a recordset for testing
//----------------------------------------
    if (FAILED(CreateTestRS(&rs)))
    {
        printf("Couldn't create the first recordset\n");
        goto exit;
    }

//----------------------------------------
//  Create IStream
//----------------------------------------
    if (FAILED(SaveRS(rs, (IStream**)&pStream)))
    {
        printf("Couldn't save the recordset\n");
        goto exit;
    }

//----------------------------------------
//  Load another recordset from IStream
//----------------------------------------
    if (FAILED(LoadRS(&rs2, pStream)))
    {
        printf("Couldn't save the recordset\n");
        goto exit;
    }
    //Now display the names of the fields of the rs that we just recreated
    {
        for (short i =0;i<rs2->Fields->Count;i++)
        printf("Name of field %d is %s\n", i, (LPCTSTR) rs2->Fields->GetItem(i)->Name);
    }



//----------------------------------------
//  Pause and then exit so that the user
//  Can  
//----------------------------------------

exit:
    printf("Press any key to end program\n");
    _getch();
    return 0;
}


//----------------------------------------
//  Create a recordset from scratch
//----------------------------------------

HRESULT CreateTestRS(_Recordset** prs/*OUT*/)
{
    try
    {
    * prs=NULL;
    _RecordsetPtr pRS;
    pRS.CreateInstance( __uuidof(Recordset));
    pRS->CursorLocation = adUseClient;
    pRS->CursorType = adOpenStatic;
    pRS->LockType = adLockBatchOptimistic;
    // append fields
    pRS->Fields->Append(_bstr_t("ADOField1"), adVarChar, 45,adFldFixed);
    pRS->Fields->Append(_bstr_t("ADOField2"), adBoolean, 0,adFldFixed);
    pRS->Fields->Append(_bstr_t("ADOField3"), adCurrency, 0,adFldFixed);
    pRS->Fields->Append(_bstr_t("ADOField4"), adDate, 0,adFldFixed);
    pRS->Open(vtMissing, vtMissing, adOpenStatic,adLockBatchOptimistic,-1);
    *prs= pRS.Detach();
    }
    catch (_com_error & e)
    {
    return e.Error();
    }
    return S_OK;
}

HRESULT SaveRS(_RecordsetPtr pRS/*IN*/, IStream* * ppStream/*OUT*/)
{
    HRESULT hr=S_OK;
    try
    {
    *ppStream=NULL;

        // QI and return IPersistStream
        IPersistStreamPtr pIPersist(pRS);
        if (pIPersist )
        {
            //Create a standard stream in memory
            if (FAILED(hr=CreateStreamOnHGlobal(0, TRUE, (IStream **)ppStream)))
        return hr;

            // Persist the pRS
            if (FAILED(hr=OleSaveToStream(pIPersist, *ppStream)))
        return hr;

        }
        else
            return E_NOINTERFACE;
    }
    catch (_com_error & e)
    {
    return e.Error();
    }
return S_OK;
    
}

HRESULT LoadRS(_Recordset* *ppRS/*OUT*/, IStreamPtr pStream/*IN*/)
{
    HRESULT hr=S_OK;
    try
    {
    *ppRS=NULL;
        if (NULL==pStream)
            return E_NOINTERFACE;
        // Load the pRS.
        LARGE_INTEGER li;
        li.QuadPart = 0;

    //Set the pointer to the beginning of the stream
        if (FAILED(hr=pStream->Seek(li, STREAM_SEEK_SET, 0)))
            return hr;

    if (FAILED(hr=OleLoadFromStream(pStream, 
            __uuidof(_Recordset),
                reinterpret_cast<LPVOID *>(ppRS)))
        )
            return hr;
    }
    catch (_com_error & e)
    {
    return e.Error();
    }
    return S_OK;
}
                

The following files are available for download from the Microsoft Download Center:

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services


Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

ADOPersist.cpp 4.1KB
ADOPersist.dsp 4.0KB


Keywords: kbhowto kbdownload kbdatabase kbfile KB242249