Click here to Skip to main content
Licence 
First Posted 30 Jan 2001
Views 101,725
Bookmarked 39 times

Another Simple ADO Class

By | 13 Mar 2001 | Article
Another simple yet cool ADO class for your project - from zCoders.com!
 
Part of The SQL Zone sponsored by
See Also
  • Download demo project - 16 Kb
  • Sample Image - logo-tm.gif

    Introduction

    Here is another ADO class for any database projects you may have coming up. The class is provided from the link above. The class is zipped up with the ado type library. I created this class because I couldn't find any good ado classes with disconnected recordsets in them. I hope this class helps you out. Here are the main methods:

    CRADatabase()
    ~CRADatabase()
    RAConnect(char *ConnectionString)
    RAGetDisconnectedRs(char *SqlStatement)
    RAExecuteNoRs(char *SqlStatement)
    RAExecuteRs(char *SqlStatement)

    Usage:

    	_RecordsetPtr oRs;
    	CRADatabase rad;
    
    	for (int count=0; count<1000; count++)
    	{
    
    		if (S_OK == rad.RAConnect("DSN=Database;UID=sa;PWD="))		
    		{			
    			oRs = rad.RAGetDisconnectedRs("SELECT TOP 100 * FROM orders");
    			
    			CComVariant val;
    			if (oRs)
    			{
    				while (!oRs->adoEOF)
    				{
    					val = oRs->Fields->Item[_variant_t("order_number")]->Value;
    					if (val.vt != (VT_NULL))
    					{
    						printf("%s\n",(char*)_bstr_t(val));				
    					}
    					oRs->MoveNext();
    				}
    				oRs->Release();					
    				oRs->Close();
    			}
    			
    		}
            }
    

    Here is the code (Click on link above to get zipped class!):

    // RADatabase.cpp: implementation of the CRADatabase class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "stdafx.h"
    #include "RADatabase.h"
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    CRADatabase::CRADatabase()
    {
    	try
    	{
    		// Initialize COM
    		CoInitialize(NULL);
    		// Initialize ADO
    		m_Command.CreateInstance(__uuidof(Command));
    		m_Recordset.CreateInstance(__uuidof(Recordset));
    		m_Connection.CreateInstance(__uuidof(Connection));	
    	}
    	catch(_com_error &e)
    	{
    		printf("Description = %s\n", (char*) e.Description());   
    	}
    	catch(...)
    	{
    	}
    }
    
    CRADatabase::~CRADatabase()
    {
    	// Clean up ADO objects
    	try
    	{
    		if (m_Connection)
    		{
    			if(m_Connection->State == adStateOpen)
    			{
    				m_Connection->Close();
    				m_Connection.Release();			
    			}
    		}
    		if (m_Recordset)
    		{
    			if(m_Recordset->State == adStateOpen)
    			{
    				m_Recordset->Close();
    				m_Recordset.Release();			
    			}
    		}		
    
    		m_Connection = NULL;
    		m_Command = NULL;
    		m_Recordset = NULL;
    
    		CoUninitialize();
    	}
    	catch(_com_error &e)
    	{
    		printf("Description = %s\n", (char*) e.Description());   
    	}
    	catch(...)
    	{
    	}
    }
    
    HRESULT CRADatabase::RAConnect(const char *ConnectionString)
    {
    	// Attemp a connect, reconnect, or leave if already connected
    	if (strlen(ConnectionString) > 0)
    	{
    		try
    		{
    			if (m_Connection == NULL)
    				m_Connection.CreateInstance(__uuidof(Connection));
    			if (0 == _stricmp(ConnectionString, m_ConnectionString) && 
    				m_Connection->State == adStateOpen)
    				return S_OK;
    			else
    			{
    				if (m_Connection->State == adStateOpen)
    					m_Connection->Close();
    				sprintf(m_ConnectionString, ConnectionString);
    			}
    			// Perform the connect
    			if (S_OK == m_Connection->Open((char*)m_ConnectionString, 
    				L"", 
    				L"", 
    				adModeUnknown))
    				return S_OK;
    		}
    		catch(_com_error &e)
    		{
    			printf("Description = %s\n", (char*) e.Description());   
    		}
    		catch(...)
    		{
    		}
    	}
    	return E_FAIL;
    }
    
    _RecordsetPtr CRADatabase::RAGetDisconnectedRs(const char *SqlStatement)
    {
    	// Attempt get a disconnected RS and terminate the connection to the DB
    	if (strlen(SqlStatement) > 0)
    	{		
    		try
    		{
    			if (m_Connection == NULL || m_Connection->State == adStateClosed)
    				RAConnect(m_ConnectionString);
    
    			if (m_Recordset)
    			{
    				if (m_Recordset->State == adStateOpen)
    					m_Recordset->Close();
    			}
    			else
    			{				
    				m_Recordset.CreateInstance(__uuidof(Recordset));
    			}
    
    			m_Recordset->CursorLocation = adUseClient;
    			
    			m_Recordset->Open(SqlStatement,
    				m_Connection.GetInterfacePtr(),
    				adOpenForwardOnly, 
    				adLockBatchOptimistic, 
    				-1);
    
    			m_Recordset->PutRefActiveConnection(NULL); 
    			
    			if(m_Connection->GetState() == adStateOpen)
    				m_Connection->Close();
    			//m_Connection.Release();
    			//m_Connection = NULL;		
    						
    			return m_Recordset.Detach();			
    		}
    		catch(_com_error &e)
    		{
    			printf("Description = %s\n", (char*) e.Description());   
    		}
    		catch(...)
    		{
    		}
    	}
    	return NULL;
    }
    
    
    HRESULT CRADatabase::RAExecuteNoRs(const char *SqlStatement)
    {
    	// Attempt to execute a SQL statement and leave connection open
    	if (strlen(SqlStatement) > 0)
    	{		
    		try
    		{
    			if (m_Connection == NULL || m_Connection->State == adStateClosed)
    				RAConnect(m_ConnectionString);
    
    			m_Connection->Execute(SqlStatement, NULL, adExecuteNoRecords );
    			
    			return S_OK;
    		}
    		catch(_com_error &e)
    		{
    			printf("Description = %s\n", (char*) e.Description());   
    		}
    		catch(...)
    		{
    		}
    	}
    	return E_FAIL;
    }
    
    _RecordsetPtr CRADatabase::RAExecuteRs(const char *SqlStatement)
    {
    	// Attempt to execute a SQL statement, return the RS, and leave connection open
    	if (strlen(SqlStatement) > 0)
    	{		
    		try
    		{
    			if (m_Connection == NULL || m_Connection->State == adStateClosed)
    				RAConnect(m_ConnectionString);
    			if (m_Recordset)
    			{
    				if (m_Recordset->State == adStateOpen)
    					m_Recordset->Close();
    			}
    			else
    			{				
    				m_Recordset.CreateInstance(__uuidof(Recordset));
    			}
    			
    			m_Command->ActiveConnection = m_Connection;
    			m_Command->CommandType = adCmdText;
    			m_Command->CommandText = SqlStatement;
    			
    			_variant_t vNull;
    			vNull.vt = VT_ERROR;
    			vNull.scode = DISP_E_PARAMNOTFOUND;
    			
    			m_Recordset = m_Command->Execute( &vNull, &vNull, adCmdText );
    		
    			
    			return m_Recordset.Detach();
    		}
    		catch(_com_error &e)
    		{
    			printf("Description = %s\n", (char*) e.Description());   
    		}
    		catch(...)
    		{
    		}
    	}
    
    	return NULL;
    }
    
    

    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

    Christopher W. Backen



    United States United States

    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
    Questionhow about record set that is not using any database? Pinmembernovice5123:53 28 Feb '11  
    Generalvc project Pinmembersarwatyounis19:37 12 Dec '04  
    GeneralFew questions PinmemberJónas Sig21:45 19 May '03  
    GeneralBAD Memory Leak!! PinsussStriker97:00 25 Oct '02  
    GeneralRe: BAD Memory Leak!! PinsussStriker97:18 20 Nov '02  
    GeneralDoes this bug in CRADatabase::RAExecuteRs() ? PinmemberEcho Yuan22:44 21 Aug '03  
    QuestionHow about Binary PinmemberAnonymous4:23 25 Mar '01  
    Thanks a lot for your great code.
    I want to store the image to db,
    how should i do by using ado?
    AnswerRe: How about Binary PinmemberJose Cruz7:56 21 Nov '02  

    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.120529.1 | Last Updated 14 Mar 2001
    Article Copyright 2001 by Christopher W. Backen
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid