HOWTO: Implement Visual C++ COM Objects Returning RecordsetsID: Q225131
|
This article describes, by example, how to implement a Visual C++ Component Object Model (COM) object that returns a recordset to Active Server Pages (ASP).
Implementing this incorrectly can result in memory leaks or one of the following errors:
The operation requested by the application is not allowed if the object is closed.
-or-
Type Mismatch
-or-
error 'ASP 0115' - A trappable error occured in an external object
Use the following steps to implement a method that returns a recordset from a Visual C++ COM object to Active Server Pages.
Method Name: TestMethod
Parameters : [out, retval] LPDISPATCH* ppRecordset
#import "msado15.dll" no_namespace rename("EOF", "adoEOF")
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;
}
<%
Dim rsTest, oTestPassRs
Set oTestPassRs = Server.CreateObject("PassRs.PassRsObj")
Set rsTest = oTestPassRs.TestMethod()
Do
Response.Write ( "Value in Record = " & rsTest(1) & "<BR>" )
rsTest.MoveNext
Loop until rsTest.EOF
rsTest.Close
Set rsTest = Nothing
Set oTestPassRs = Nothing
%>
For additional information, please see the following articles in the Microsoft Knowledge Base:
Q184397 HOWTO: Getting ADO Disconnected Recordsets in VBA/C++/Java
Q193515 PRB: Update of Same Record 2x w/Disconnect ADO Recordset Fails
Additional query words:
Keywords : kbADO150 kbADO200 kbCOMt kbVC500 kbVC600 kbGrpASP
Version : WINDOWS:1.5,2.0; winnt:5.0,6.0
Platform : WINDOWS winnt
Issue type : kbhowto
Last Reviewed: August 8, 1999