COM Object Returning Recordset

This article has two parts. First how to develop a COM object which returns recordsets, and second part is an ASP client which let you use the object.  

Use the following steps to implement a method that returns a recordset from a Visual C++ COM object to Active Server Pages.

Create an ATL DLL project called PassRs. 

Insert an ATL object named PassRsObj. 

Add a method with the following information:

Method Name: TestMethod

Parameters : [out, retval] LPDISPATCH* ppRecordset 

Include the following line in the object's implementation file:

#import "msado15.dll" no_namespace rename("EOF", "adoEOF") 

Implement the method as follows:

STDMETHODIMP CPassRsObj::TestMethod(LPDISPATCH *ppRecordset )

{

    _ConnectionPtr pConn;

    _RecordsetPtr pRs;

    pConn.CreateInstance(__uuidof(Connection));

    pRs.CreateInstance(__uuidof(Recordset));

    pConn->Open("DSN=pubs;uid=sa;pwd=;", (BSTR) NULL, (BSTR) NULL, -1);

    //Client side cursor is required for disconnected recordsets

    pRs->CursorLocation = adUseClient;

    pRs->Open( "select * from authors",

               pConn.GetInterfacePtr(),

               adOpenKeyset, adLockOptimistic, -1);

    // Disconnect the recordset

    pRs->PutRefActiveConnection(NULL);

    //Clone the recordset. 

    //NOTE: Recordset to be cloned must support bookmarks

    pRs->Clone(adLockOptimistic)->QueryInterface(IID_IDispatch, (void**) ppRecordset);

    pRs->Close();

    pConn->Close();

    pRs = NULL;

    pConn = NULL; 

    return S_OK;

}  

 

Create an ASP page with the following code: 

<%

Dim rsTest, oTestPassRs

Set oTestPassRs = Server.CreateObject("PassRs.PassRsObj")

Set rsTest = oTestPassRs.TestMethod()

Do 

   Response.Write ( "Value in Record = " & rsTest(1) & "
" )

   rsTest.MoveNext

Loop until rsTest.EOF

rsTest.Close

Set rsTest = Nothing

Set oTestPassRs = Nothing

%>