Click here to Skip to main content
Click here to Skip to main content

How to return Disconnected ADO Recordset from COM

, 18 Mar 2002
Rate this:
Please Sign up or sign in to vote.
An article on how to return disconnected ADO Recordset from COM using VC.
<!-- Download Links --> <!-- Add the rest of your HTML here -->

Introduction

Some business objects return ADO recordsets to their client. This is a common practice when you are developing a 3-tier web application. You need to process your business logic in your object or stored procedure and then return the results/resultset to the presentation layer. Very often, you need to return the resultset to the client. So that the presentation layer can process the resultset. (e.g:converting to XML, displaying the results). While the presentation layer is doing the processing, it is good to disconnect it from SQL Server to conserve database resource. (e.g: disconnected recordset)

Implementation Details

First you have to configure Visual C++ to use platform SDK. Select Options from the Tools Menu. Click the Directories tab.

  • Add an entry for the Platform SDK include directory. (e.g: c:\platform sdk\include)
  • Add an entry for the ATL 3.0 include directory. (e.g: c:\platform sdk\include\atl30)
  • Add an entry for the library directory of the Platform SDK. (e.g: c:\platform sdk\lib)
  • Add an entry for the Bin directory of the Platform SDK. (e.g: c:\platform sdk\bin) (Make sure that these entries are first in the list)

Next you have to configure your project to work with ADO. The easiest way to work with ADO in Visual C++ is to use native COM support to import the ADO type library and generate smart pointer wrapper classes for the objects in the ADO library: Recordset, Connection, Command and so on. Add the following #import statement to the stdafx.h file in your project:

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF", "adoEOF")

This assumes that your msado15.dll resides in c:\Program Files\Common Files\System\ADO\msado15.dll. Create a method to the Interface.

Method name: GetEmployeeList.

Parameters: [out, retval] _Recordset **returnvalue

STDMETHODIMP CAdoRec::GetEmployeeList(_Recordset **returnvalue)

  {

  _ConnectionPtr pConnection = NULL;

  _CommandPtr pCommand = NULL;

  _RecordsetPtr pRecordset = NULL;
 _bstr_t sql("select lastname,firstname,employeeid from employees");


  try

  {

  pConnection.CreateInstance(__uuidof(Connection));

  pConnection->Open(CONNECTION_STRING,"","",-1);

  pRecordset.CreateInstance(__uuidof(Recordset));

  pRecordset->CursorLocation=adUseClient;

  pRecordset->Open(sql,pConnection.GetInterfacePtr(), 
    adOpenStatic,adLockBatchOptimistic,-1);  

  pRecordset->AddRef();

  *returnvalue = pRecordset;

  pRecordset->putref_ActiveConnection(NULL);
}

  catch(_com_error err)

  {
    pConnection->Close();
 }  

  pConnection->Close();
 return S_OK;

  }

You have to add reference to the recordset and then return it. After that you disconnect the recordset from the database connection. You can modify the code to call a stored procedure instead.

You also have to import the ADO type library into your IDL file. Use importlib as shown below.

  import "oaidl.idl";

  import "ocidl.idl";

  [

  uuid(AAC8075D-21AD-46B2-A1BE-9777DE78B4DC),

  version(1.0),

  helpstring("ADOrecordset 1.0 Type Library")

  ]

  library ADORECORDSETLib

  {

  importlib("stdole32.tlb");

  importlib("stdole2.tlb");

  importlib("c:\Program Files\Common Files\System\ADO\msado15.dll");	
   
 [

  uuid(DB761CAF-9C4A-42ED-9654-F963552F3FA4),

  helpstring("AdoRec Class")

  ]

  coclass AdoRec

  {

  [default] interface IAdoRec;

  };

  [

  object,

  uuid(E086C0B9-52EC-4F45-AE9D-03BB9CA5FE84),

  dual,

  helpstring("IAdoRec Interface"),

  pointer_default(unique)

  ]

  interface IAdoRec : IDispatch

  {

  [id(1), helpstring("method GetEmployeeList")] 
    HRESULT GetEmployeeList([out, 
    retval] _Recordset **returnvalue);

  };

  };


This is how you can call the GetEmployeeList method to return a disconnected recordset to ASP. Note that objPagingRS is the disconnected recordset. You can manulate it just like a normal recordset. For illustration purpose, I am just rendering the first field of the recordset on the asp page.

<%

  set fbn=server.CreateObject("adorecordset.adorec")

  set objPagingRS=fbn.GetEmployeeList()

  set fbn=nothing

  do while Not objPagingRS.EOF

  Response.Write "<BR>" + cstr(objPagingRS.Fields(1)) + vbCrLf

  objPagingRS.MoveNext

  loop

  objPagingRS.Close

  Set objPagingRS = Nothing

  %>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Siau Tan Long
Web Developer
Singapore Singapore
No Biography provided

Comments and Discussions

 
GeneralA problem occured when using ADO in C++ and user disable network many times PinmemberTam Ho6-Mar-05 17:07 
GeneralRe: A problem occured when using ADO in C++ and user disable network many times PinmemberVitalyTomilov30-Jun-05 1:48 
GeneralFinding the dynamic columns from the database - Urgent PinmemberKoundinya8-Nov-04 10:07 
GeneralRe: Finding the dynamic columns from the database - Urgent PinmemberJohn M. Drescher8-Nov-04 13:33 
GeneralRe: Finding the dynamic columns from the database - Urgent PinmemberVitalyTomilov30-Jun-05 1:22 
GeneralRe: Finding the dynamic columns from the database - Urgent PinmemberJohn M. Drescher30-Jun-05 4:28 
GeneralRe: Finding the dynamic columns from the database - Urgent PinmemberVitalyTomilov30-Jun-05 1:27 
GeneralWhen to use disconnected recordset Pinmembernavinkaus30-Sep-04 20:40 
GeneralRe: When to use disconnected recordset PinmemberSiau Tan Long30-Sep-04 21:33 
GeneralRe: When to use disconnected recordset Pinmembernavinkaus30-Sep-04 21:36 
GeneralRe: When to use disconnected recordset PinmemberSiau Tan Long30-Sep-04 22:20 
GeneralDetach() instead AddRef() Pinmemberspod letela19-Mar-02 6:35 
GeneralRe: Detach() instead AddRef() PinmemberSiau Tan Long20-Mar-02 1:56 
GeneralRe: Detach() instead AddRef() PinmemberRashid Thadha20-Mar-02 5:02 
GeneralAddRef or QueryInterface PinmemberRashid Thadha19-Mar-02 5:19 
GeneraladUseServer and adUseClient PinmemberMazdak19-Mar-02 4:22 
GeneralRe: adUseServer and adUseClient PinmemberSiau Tan Long19-Mar-02 15:23 
GeneralRe: adUseServer and adUseClient PinmemberMazdak19-Mar-02 20:41 
GeneralRe: adUseServer and adUseClient PinmemberSiau Tan Long20-Mar-02 2:04 
GeneralRe: adUseServer and adUseClient PinmemberMazdak20-Mar-02 3:16 
GeneralRe: adUseServer and adUseClient Pinmemberspod letela25-Mar-02 8:03 
GeneralRe: adUseServer and adUseClient PinmemberMazdak26-Mar-02 1:35 
GeneralRe: adUseServer and adUseClient Pinmemberlipinshengxp8-Mar-04 20:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 19 Mar 2002
Article Copyright 2002 by Siau Tan Long
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid