How to implement a Visual C + + COM object that returns a recordset

Source: Internet
Author: User
Tags strong password

Error implementation of this object can cause a memory leak or one of the following errors:

The operation requested by the application isn't allowed if the object is closed.

Or

Type mismatch

Or

Error ' ASP 0115 '-A trappable error occured in A external object

More information

Use the following steps to implement a method that returns a recordset from a Visual C + + COM object to the Active Server Pages. 1. Create an ATL DLL project named "Passrs". 2. Insert an ATL object named "Passrsobj". 3. Add methods that contain the following information: Method Name:testmethod

Parameters: [out, retval] lpdispatch* pprecordset

4. Include the following line in the object's implementation file:#import "msado15.dll" no_namespace rename("EOF", "adoEOF")

5. Implement the method as follows:

Note: You must first change the uid=<username> and Pwd=<strong password> to the correct values before you can run this code. Make sure that the UID has the appropriate permissions required to perform this operation in 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>;p wd=<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 is cloned must support bookmarks
Prs->clone (adlockoptimistic)->queryinterface (iid_ IDispatch, (void**) pprecordset);
Prs->close ();
Pconn->close ();
PRs = NULL;
Pconn = NULL;
return S_OK;
}

6. Create an ASP page that contains 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
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.