Click here to Skip to main content
15,885,030 members
Articles / Desktop Programming / ATL

How to Return Disconnected ADO Recordset from COM

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
18 Mar 2002CPOL2 min read 141.6K   919   26   23
An article on how to return disconnected ADO Recordset from COM using VC

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:

C++
#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

C++
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:

C++
 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 manipulate it just like a normal recordset. For illustration purposes, I am just rendering the first field of the recordset on the ASP page.

VBScript
<%
  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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralA problem occured when using ADO in C++ and user disable network many times Pin
Member 17541146-Mar-05 16:07
Member 17541146-Mar-05 16:07 
GeneralRe: A problem occured when using ADO in C++ and user disable network many times Pin
Vitaly Tomilov30-Jun-05 0:48
Vitaly Tomilov30-Jun-05 0:48 
GeneralFinding the dynamic columns from the database - Urgent Pin
Koundinya8-Nov-04 9:07
Koundinya8-Nov-04 9:07 
GeneralRe: Finding the dynamic columns from the database - Urgent Pin
John M. Drescher8-Nov-04 12:33
John M. Drescher8-Nov-04 12:33 
GeneralRe: Finding the dynamic columns from the database - Urgent Pin
Vitaly Tomilov30-Jun-05 0:22
Vitaly Tomilov30-Jun-05 0:22 
GeneralRe: Finding the dynamic columns from the database - Urgent Pin
John M. Drescher30-Jun-05 3:28
John M. Drescher30-Jun-05 3:28 
GeneralRe: Finding the dynamic columns from the database - Urgent Pin
Vitaly Tomilov30-Jun-05 0:27
Vitaly Tomilov30-Jun-05 0:27 
GeneralWhen to use disconnected recordset Pin
navinkaus30-Sep-04 19:40
navinkaus30-Sep-04 19:40 
GeneralRe: When to use disconnected recordset Pin
Siau Tan Long30-Sep-04 20:33
Siau Tan Long30-Sep-04 20:33 
GeneralRe: When to use disconnected recordset Pin
navinkaus30-Sep-04 20:36
navinkaus30-Sep-04 20:36 
GeneralRe: When to use disconnected recordset Pin
Siau Tan Long30-Sep-04 21:20
Siau Tan Long30-Sep-04 21:20 
GeneralDetach() instead AddRef() Pin
19-Mar-02 5:35
suss19-Mar-02 5:35 
GeneralRe: Detach() instead AddRef() Pin
20-Mar-02 0:56
suss20-Mar-02 0:56 
GeneralRe: Detach() instead AddRef() Pin
Rashid Thadha20-Mar-02 4:02
Rashid Thadha20-Mar-02 4:02 
GeneralAddRef or QueryInterface Pin
Rashid Thadha19-Mar-02 4:19
Rashid Thadha19-Mar-02 4:19 
GeneraladUseServer and adUseClient Pin
Mazdak19-Mar-02 3:22
Mazdak19-Mar-02 3:22 
GeneralRe: adUseServer and adUseClient Pin
19-Mar-02 14:23
suss19-Mar-02 14:23 
GeneralRe: adUseServer and adUseClient Pin
Mazdak19-Mar-02 19:41
Mazdak19-Mar-02 19:41 
GeneralRe: adUseServer and adUseClient Pin
20-Mar-02 1:04
suss20-Mar-02 1:04 
GeneralRe: adUseServer and adUseClient Pin
Mazdak20-Mar-02 2:16
Mazdak20-Mar-02 2:16 
GeneralRe: adUseServer and adUseClient Pin
25-Mar-02 7:03
suss25-Mar-02 7:03 
GeneralRe: adUseServer and adUseClient Pin
Mazdak26-Mar-02 0:35
Mazdak26-Mar-02 0:35 
GeneralRe: adUseServer and adUseClient Pin
lipinshengxp8-Mar-04 19:15
lipinshengxp8-Mar-04 19:15 

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

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