Click here to Skip to main content
11,647,170 members (70,870 online)
Click here to Skip to main content

How to return Disconnected ADO Recordset from COM

, 18 Mar 2002 120.5K 824 26
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

You may also be interested in...

Comments and Discussions

 
GeneralA problem occured when using ADO in C++ and user disable network many times Pin
Tam Ho6-Mar-05 16:07
memberTam Ho6-Mar-05 16:07 
GeneralRe: A problem occured when using ADO in C++ and user disable network many times Pin
VitalyTomilov30-Jun-05 0:48
memberVitalyTomilov30-Jun-05 0:48 
GeneralFinding the dynamic columns from the database - Urgent Pin
Koundinya8-Nov-04 9:07
memberKoundinya8-Nov-04 9:07 
GeneralRe: Finding the dynamic columns from the database - Urgent Pin
John M. Drescher8-Nov-04 12:33
memberJohn M. Drescher8-Nov-04 12:33 
GeneralRe: Finding the dynamic columns from the database - Urgent Pin
VitalyTomilov30-Jun-05 0:22
memberVitalyTomilov30-Jun-05 0:22 
GeneralRe: Finding the dynamic columns from the database - Urgent Pin
John M. Drescher30-Jun-05 3:28
memberJohn M. Drescher30-Jun-05 3:28 
GeneralRe: Finding the dynamic columns from the database - Urgent Pin
VitalyTomilov30-Jun-05 0:27
memberVitalyTomilov30-Jun-05 0:27 
GeneralWhen to use disconnected recordset Pin
navinkaus30-Sep-04 19:40
membernavinkaus30-Sep-04 19:40 
GeneralRe: When to use disconnected recordset Pin
Siau Tan Long30-Sep-04 20:33
memberSiau Tan Long30-Sep-04 20:33 
GeneralRe: When to use disconnected recordset Pin
navinkaus30-Sep-04 20:36
membernavinkaus30-Sep-04 20:36 
GeneralRe: When to use disconnected recordset Pin
Siau Tan Long30-Sep-04 21:20
memberSiau Tan Long30-Sep-04 21:20 
GeneralDetach() instead AddRef() Pin
spod letela19-Mar-02 5:35
memberspod letela19-Mar-02 5:35 
GeneralRe: Detach() instead AddRef() Pin
Siau Tan Long20-Mar-02 0:56
memberSiau Tan Long20-Mar-02 0:56 
GeneralRe: Detach() instead AddRef() Pin
Rashid Thadha20-Mar-02 4:02
memberRashid Thadha20-Mar-02 4:02 
GeneralAddRef or QueryInterface Pin
Rashid Thadha19-Mar-02 4:19
memberRashid Thadha19-Mar-02 4:19 
GeneraladUseServer and adUseClient Pin
Mazdak19-Mar-02 3:22
memberMazdak19-Mar-02 3:22 
GeneralRe: adUseServer and adUseClient Pin
Siau Tan Long19-Mar-02 14:23
memberSiau Tan Long19-Mar-02 14:23 
GeneralRe: adUseServer and adUseClient Pin
Mazdak19-Mar-02 19:41
memberMazdak19-Mar-02 19:41 
GeneralRe: adUseServer and adUseClient Pin
Siau Tan Long20-Mar-02 1:04
memberSiau Tan Long20-Mar-02 1:04 
GeneralRe: adUseServer and adUseClient Pin
Mazdak20-Mar-02 2:16
memberMazdak20-Mar-02 2:16 
GeneralRe: adUseServer and adUseClient Pin
spod letela25-Mar-02 7:03
memberspod letela25-Mar-02 7:03 
GeneralRe: adUseServer and adUseClient Pin
Mazdak26-Mar-02 0:35
memberMazdak26-Mar-02 0:35 
GeneralRe: adUseServer and adUseClient Pin
lipinshengxp8-Mar-04 19:15
memberlipinshengxp8-Mar-04 19: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
Web01 | 2.8.150804.2 | Last Updated 19 Mar 2002
Article Copyright 2002 by Siau Tan Long
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid