Click here to Skip to main content
Licence 
First Posted 18 Mar 2002
Views 111,177
Bookmarked 26 times

How to return Disconnected ADO Recordset from COM

By | 18 Mar 2002 | Article
An article on how to return disconnected ADO Recordset from COM using VC.
 
Part of The SQL Zone sponsored by
See Also

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

About the Author

Siau Tan Long

Web Developer

Singapore Singapore

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralA problem occured when using ADO in C++ and user disable network many times PinmemberTam Ho16:07 6 Mar '05  
GeneralRe: A problem occured when using ADO in C++ and user disable network many times PinmemberVitalyTomilov0:48 30 Jun '05  
GeneralFinding the dynamic columns from the database - Urgent PinmemberKoundinya9:07 8 Nov '04  
GeneralRe: Finding the dynamic columns from the database - Urgent PinmemberJohn M. Drescher12:33 8 Nov '04  
GeneralRe: Finding the dynamic columns from the database - Urgent PinmemberVitalyTomilov0:22 30 Jun '05  
GeneralRe: Finding the dynamic columns from the database - Urgent PinmemberJohn M. Drescher3:28 30 Jun '05  
GeneralRe: Finding the dynamic columns from the database - Urgent PinmemberVitalyTomilov0:27 30 Jun '05  
GeneralWhen to use disconnected recordset Pinmembernavinkaus19:40 30 Sep '04  
GeneralRe: When to use disconnected recordset PinmemberSiau Tan Long20:33 30 Sep '04  
GeneralRe: When to use disconnected recordset Pinmembernavinkaus20:36 30 Sep '04  
GeneralRe: When to use disconnected recordset PinmemberSiau Tan Long21:20 30 Sep '04  
GeneralDetach() instead AddRef() Pinmemberspod letela5:35 19 Mar '02  
GeneralRe: Detach() instead AddRef() PinmemberSiau Tan Long0:56 20 Mar '02  
GeneralRe: Detach() instead AddRef() PinmemberRashid Thadha4:02 20 Mar '02  
GeneralAddRef or QueryInterface PinmemberRashid Thadha4:19 19 Mar '02  
GeneraladUseServer and adUseClient PinmemberMazdak3:22 19 Mar '02  
GeneralRe: adUseServer and adUseClient PinmemberSiau Tan Long14:23 19 Mar '02  
GeneralRe: adUseServer and adUseClient PinmemberMazdak19:41 19 Mar '02  
GeneralRe: adUseServer and adUseClient PinmemberSiau Tan Long1:04 20 Mar '02  
GeneralRe: adUseServer and adUseClient PinmemberMazdak2:16 20 Mar '02  
GeneralRe: adUseServer and adUseClient Pinmemberspod letela7:03 25 Mar '02  
GeneralRe: adUseServer and adUseClient PinmemberMazdak0:35 26 Mar '02  
GeneralRe: adUseServer and adUseClient Pinmemberlipinshengxp19:15 8 Mar '04  

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.

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