Creating a Disconnected Recordset in C++ For VB | CodeGuru

Creating a Disconnected Recordset in C++ For VB

Environment: Visual C++ 6.0 and VB 6.0, ADO 1.5 This article assumes that you are familiar with ADO, Visual C++ and ATL. This article shows how to create a Visual C++ COM object that creates a disconnected ADO Recordset. With a disconnected ADO recordset, your ATL COM object can connect to the database, create the […]

Written By
CodeGuru Staff
CodeGuru Staff
May 21, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: Visual C++ 6.0 and VB 6.0, ADO 1.5

This article assumes that you are familiar with ADO, Visual C++ and ATL.

This article shows how to create a Visual C++ COM object that creates a disconnected ADO Recordset. With a disconnected ADO recordset, your ATL COM object can connect to the database, create the Recordset, and disconnect without having to keep a connection open, while allowing the client to read the data in the Recordset. This works great with MTS where database connection pooling requires this functionality.

Resources:
HOWTO: Getting ADO Disconnected Recordsets in VBA/C++/Java

SAMPLE: ATL2ADO.exe Returns Disconnected ADO Recordset

In your project make sure you import dll to create your ADO classes.

#import “c:program filescommon filessystemadomsado15.dll” no_namespace rename(“EOF”, “adoEOF”) implementation_only

Next create a method with an [out, retval] property of type _Recordset **

[id(1), helpstring(“method GetADORecordset”)] HRESULT GetADORecordset( [out, retval] _Recordset **ppRecordset);

Your source code should look something like this:

STDMETHODIMP CReturnADO::GetADORecordset(_Recordset **ppRecordset)
{
	_ConnectionPtr	pConnection;
	_RecordsetPtr	pRecordset;
	try
	{
		pConnection.CreateInstance(__uuidof(Connection));
		pConnection->CursorLocation = adUseClient; // Important for disconnected sets
		pConnection->Open(“DSN=SQLSource”,”loginnmae”,”password”,-1);
		pRecordset.CreateInstance(__uuidof(Recordset));
		pRecordset->CursorLocation = adUseClient; // Important for disconnected sets
		pRecordset->PutRefActiveConnection(pConnection.GetInterfacePtr()); // Associate the connection to this recordset
		pRecordset->Open(“SELECT DISTINCT name FROM TBLWHATEVER WHERE name IS NOT NULL”,vtMissing,
			adOpenForwardOnly, adLockReadOnly,
			adCmdText);
        pRecordset->PutRefActiveConnection(NULL); // Disassociate the connection from the recordset.
		if(pConnection->GetState() == adStateOpen)
			pConnection->Close();
		pConnection = NULL;
		*ppRecordset = pRecordset.Detach();
	}
	catch(_com_error *e)
	{
		return AtlReportError(CLSID_ReturnADO,e->ErrorMessage(),IID_IReturnADO,E_FAIL);
	}
	catch(…)
	{
		return AtlReportError(CLSID_ReturnADO,”Unknown Error.”,IID_IReturnADO,E_FAIL);
	}
	return S_OK;
}

Now your interface descriptions and CLSID’s will be different, but go ahead and use this code to build your own version. Your VB code might look something like this:

    Dim adoset As ADODB.Recordset
    Dim engine As New TESTRETURNADOLib.ReturnADO
    Set adoset = engine.GetADORecordset
    While Not adoset.EOF
        List1.AddItem adoset.Fields(0)
        adoset.MoveNext
    Wend
    Set adoset = Nothing

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.