Click here to Skip to main content
15,886,518 members
Articles / Database Development / SQL Server

Generic Database Class Using ADO to Execute Stored Procedures

Rate me:
Please Sign up or sign in to vote.
3.50/5 (7 votes)
3 May 2001 93.8K   4.1K   37  
Use this class to separate all database calls hidden to the callers. Nobody has to take care of how database retrieval is going on!
/***************************************************************
 *  Author:  Amir Samdani
 *
 *  Description: Implementation for the CDatabaseServer class.
 *				 Generaric class To manage database related operations
 * 
 *  Date: 5 July 2000
 *
 *  Module Name:  DatabaseServer.h
 *
 *  Code Updations Come here 
 *
 *     SNO		Who				When           What
 *     ----     -------			-----          -----------
 ****************************************************************/

#include "stdafx.h"
#include "DatabaseServer.h"
#include <stdarg.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDatabaseServer::CDatabaseServer()
{
	
}

CDatabaseServer::~CDatabaseServer()
{

}

bool CDatabaseServer::getConnectionString(char *szConnectionString)
{
	char szServerName[MAX_COMPUTERNAME_LENGTH + 1];
	DWORD dwSize=sizeof(szServerName) ;
	if(!GetComputerName(szServerName,&dwSize))
		return false ;


	if(!szConnectionString)
		return false ;

	char szUserName[] = "SA";
	char szPassword[] = "";
	char szDatabase[] = "IQRA_DIPLOMA";
	
	sprintf(szConnectionString,"provider=sqloledb;data source=%s;initial catalog=%s;uid=%s;pwd=%s;",szServerName,szDatabase,szUserName,szPassword); 

	return true;
}

VARIANT CDatabaseServer::getExecStoredProcedure(char *szTypes,SAFEARRAY *pSPFields)
{
    _variant_t vtResultRows;
    try
    {
		_CommandPtr   pCmdPtr;
        _RecordsetPtr pRecordset;
        HRESULT hr ; 

		hr = pCmdPtr.CreateInstance(__uuidof(Command));

		char szConnectionString[255];
		getConnectionString(szConnectionString);
		_variant_t vtConnectionString(szConnectionString);
		pCmdPtr->put_ActiveConnection(vtConnectionString);

        pCmdPtr->CommandType = adCmdStoredProc;
		pCmdPtr->CommandText =  szTypes ;
		hr = pCmdPtr->Parameters->Refresh();

	   long lBound,uBound ;
	   HRESULT hresult ;
	   // Getting Safe Array's Lower and Upper Bounds
	   hresult = SafeArrayGetLBound(pSPFields, 1, &lBound);
   	   hresult = SafeArrayGetUBound(pSPFields, 1, &uBound);

	   variant_t vtParamVal;
		_variant_t Index;
		Index.vt = VT_I2;
		Index.iVal = 1 ;
	   for (long iElements=lBound;iElements<=uBound;iElements++)
	   {
			hresult = SafeArrayGetElement(pSPFields, &iElements, &vtParamVal);
			pCmdPtr->GetParameters()->GetItem(Index)->PutValue(vtParamVal) ;
			Index.iVal++ ;
	   }

   		//Execute current Stored Procedure
        _variant_t vEffected ;
		pRecordset = pCmdPtr->Execute(&vEffected,NULL,NULL);
		if (pRecordset->BOF || pRecordset->EndOfFile)
			throw ;
		// Get result set in the form of array
        vtResultRows = pRecordset->GetRows(-1);
        return vtResultRows.Detach() ;
	}
	catch(_com_error &e)
    {
		ATLTRACE((LPCSTR)e.Description());
    }
	vtResultRows.vt = VT_EMPTY ;
    return vtResultRows.Detach();
}

long CDatabaseServer::setExecStoredProcedure(char *szTypes,SAFEARRAY *pSPFields)
{	
    _variant_t vtResultRows;
    try
    {
        _CommandPtr   pCmdPtr;
        _RecordsetPtr pRecordset;
        HRESULT hr ; 

		hr = pCmdPtr.CreateInstance(__uuidof(Command));

		char szConnectionString[255];
		getConnectionString(szConnectionString);
		_variant_t vtConnectionString(szConnectionString);
		pCmdPtr->put_ActiveConnection(vtConnectionString);

        pCmdPtr->CommandType = adCmdStoredProc;
		pCmdPtr->CommandText =  szTypes ;
		hr = pCmdPtr->Parameters->Refresh();

	   long lBound,uBound;
	   HRESULT hresult;
	   // Getting Safe Array's Lower and Upper Bounds
	   hresult = SafeArrayGetLBound(pSPFields, 1, &lBound);
   	   hresult = SafeArrayGetUBound(pSPFields, 1, &uBound);

	   variant_t vtParamVal;
		_variant_t Index;
		Index.vt = VT_I2;
		Index.iVal = 1 ;
	   for (long iElements=lBound;iElements<=uBound;iElements++)
	   {
			hresult = SafeArrayGetElement(pSPFields, &iElements, &vtParamVal);
			pCmdPtr->GetParameters()->GetItem(Index)->PutValue(vtParamVal) ;
			Index.iVal++ ;
	   }

        _variant_t vEffected ;
		pCmdPtr->Execute(&vEffected,NULL,NULL);
		
		// We Are Expecting That Stored Procedures Return ID for Entity to which 
		// NSERT/UPDATE/DELETE  operation is being performed
        return (long)pCmdPtr->Parameters->Item["RETURN_VALUE"]->Value  ;
    }
    catch(_com_error &e)
    {
       ATLTRACE((LPCSTR)e.Description());
    }
    return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Team Leader
United Kingdom United Kingdom
.Over nine years work experience in different area of Software Engineering especially developing solutions in Visual C++, Visual Basic 6.0 and C# with multi tier architecture.
.Proficient in object-oriented analysis, design and programming especially in distributed client/Server environments.
.Extensive experience in WEB based software engineering using ASP.NET/ASP.
.Vast experience in designing/developing/implementing complex business applications in banking domain.
.Good work experience in Database programming using stored procedures, triggers and generating complex queries using Oracle and SQL Server 2005.

Comments and Discussions