Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 225131 - Last Review: July 1, 2004 - Revision: 4.2
How To Implement Visual C++ COM Objects Returning Recordsets
This article was previously published under 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-
-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.
- 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:
Note You must change UID=<username>
and pwd=<strong password> to the correct values before you run this
code. Make sure that UID has the appropriate permissions to perform this
operation on the database.
STDMETHODIMP CPassRsObj::TestMethod(LPDISPATCH *ppRecordset )
{
_ConnectionPtr pConn;
_RecordsetPtr pRs;
pConn.CreateInstance(__uuidof(Connection));
pRs.CreateInstance(__uuidof(Recordset));
pConn->Open("DSN=pubs;uid=<username>;pwd=<strong password>;", (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) & "<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:
184397Â
(http://kbalertz.com/Feedback.aspx?kbNumber=184397/EN-US/
)
How To Getting ADO Disconnected Recordsets in VBA/C++/Java
193515Â
(http://kbalertz.com/Feedback.aspx?kbNumber=193515/EN-US/
)
PRB: Update of Same Record 2x w/Disconnect ADO Recordset Fails
224424Â
(http://kbalertz.com/Feedback.aspx?kbNumber=224424/EN-US/
)
How To Implement Visual Basic COM Objects Returning Recordsets
APPLIES TO
- Microsoft Active Server Pages 4.0
- Microsoft ActiveX Data Objects 2.7
- Microsoft Visual C++ 5.0 Enterprise Edition
- Microsoft Visual C++ 6.0 Enterprise Edition
- Microsoft Visual C++ 5.0 Professional Edition
- Microsoft Visual C++ 6.0 Professional Edition
- Microsoft Visual C++, 32-bit Learning Edition 6.0
- Microsoft Internet Information Server 4.0
- Microsoft Internet Information Services 5.0
- Microsoft Data Access Components 2.5
| kbcodesnippet kbdatabase kberrmsg kbhowto KB225131 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate