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 recordset
s 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 the link above to get zipped class!):
#include "stdafx.h"
#include "RADatabase.h"
CRADatabase::CRADatabase()
{
try
{
CoInitialize(NULL);
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()
{
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)
{
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);
}
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)
{
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();
return m_Recordset.Detach();
}
catch(_com_error &e)
{
printf("Description = %s\n", (char*) e.Description());
}
catch(...)
{
}
}
return NULL;
}
HRESULT CRADatabase::RAExecuteNoRs(const char *SqlStatement)
{
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)
{
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;
}