CADO 2
CADO 2 file .ccp
#include "stdafx.h" #include "ADO.h"
CADO::CADO() { m_pConn = NULL; m_bIsConnected = NULL; m_pRset = NULL; m_hresult = S_OK;
}
CADO::~CADO() { if(m_bIsConnected) { m_pConn->Close(); CoInitialize(NULL); } }
//***************************************************************//
#include "stdafx.h" #include "ADO.h"
CADO::CADO() { m_pConn = NULL; m_bIsConnected = NULL; m_pRset = NULL; m_hresult = S_OK;
}
CADO::~CADO() { if(m_bIsConnected) { m_pConn->Close(); CoInitialize(NULL); } }
//***************************************************************//
BOOL CADO::Connect(char *szDBServerIP, char *szDBName, char *szUserID, char *szUserPW)
{
if(m_bIsConnected)
{
printf("DB is already connected!\n");
return TRUE;
}
char szCon[256];
memset(szCon, 0x00, sizeof(szCon));
sprintf(szCon, "Provider=SQLOLEDB;Data Source=%s; Initial catalog=%s;User ID=%s;Password=%s;", szDBServerIP, szDBName, szUserID, szUserPW);
//Initialize the COM Library
CoInitialize(NULL);
m_hresult = m_pConn.CreateInstance((__uuidof(Connection)));
if(FAILED(m_hresult))
{
printf("Error instantiating Connection object\n");
CoUninitialize();
return FALSE;
}
m_hresult = m_pConn->Open(szCon,"","",0);
if(FAILED(m_hresult))
{
printf("Error Opening Database object using ADO _ConnectionPtr \n");
CoUninitialize();
return FALSE;
}
m_bIsConnected = TRUE;
return TRUE;
}
//***************************************************************//
BOOL CADO::DisConnect()
{
if(!m_bIsConnected)
{
printf("DB is already disconnected!\n");
return TRUE;
}
m_pConn->Close();
CoUninitialize();
m_bIsConnected = FALSE;
return TRUE;
}
//***************************************************************//
BOOL CADO::SQLInsert(char* szTableName, char* szValues)
{
if(!m_bIsConnected)
{
printf("DB is disconnected!\n");
return FALSE;
}
try
{
char szSQL[256];
memset(szSQL, 0x00, sizeof(szSQL));
sprintf(szSQL, "Insert into %s Values(%s)", szTableName, szValues);
//Execute the insert statement
m_pConn->Execute(szSQL,NULL,adExecuteNoRecords);
}
catch(...)
{
return FALSE;
}
return TRUE;
}
//***************************************************************//
BOOL CADO::SQLInsert(char* szSQL)
{
if(!m_bIsConnected)
{
printf("DB is disconnected!\n");
return FALSE;
}
try
{
//Execute the insert statement
m_pConn->Execute(szSQL,NULL,adExecuteNoRecords);
}
catch( ... )
{
return FALSE;
}
return TRUE;
}
//***************************************************************//
_RecordsetPtr CADO::SQLSelect(char *fieldStmt,char *szTableName,char *WhereStmt)
{
if(!m_bIsConnected)
{
printf("DB is disconnected!\n");
return 0;
}
try
{
char szSQL[256];
memset(szSQL, 0x00, sizeof(szSQL));
//sprintf(szSQL, "select * from %s", szTableName );
sprintf( szSQL, "SELECT %s FROM %s WHERE %s", fieldStmt, szTableName, WhereStmt );
m_pRset = m_pConn->Execute(szSQL, NULL, adCmdText);
}
catch(...)
{
throw;
return 0;
}
return m_pRset;
}
//***************************************************************//
BOOL CADO::SQLUpdate(char *szTableName, char *szSet, char *szWhere)
{
if(!m_bIsConnected)
{
printf("DB is disconnected!\n");
return FALSE;
}
try
{
char szSQL[256];
memset(szSQL, 0x00, sizeof(szSQL));
sprintf(szSQL, "UPDATE %s set %s where %s", szTableName, szSet, szWhere);
// m_pConn->BeginTrans();
m_pConn->Execute(szSQL,NULL,adExecuteNoRecords);
// m_pConn->CommitTrans();
}
catch(...)
{
return FALSE;
}
return TRUE;
}
//***************************************************************//
BOOL CADO::SQLDelete(char *szTableName, char *szWhere)
{
if(!m_bIsConnected)
{
printf("DB is disconnected!\n");
return FALSE;
}
try
{
char szSQL[256];
memset(szSQL, 0x00, sizeof(szSQL));
sprintf(szSQL, "DELETE from %s where %s", szTableName, szWhere);
//m_pConn->BeginTrans();
m_pConn->Execute(szSQL,NULL,adExecuteNoRecords);
//m_pConn->CommitTrans();
}
catch(...)
{
return FALSE;
}
return TRUE;
}
//***************************************************************//
void* CADO::SQLExcute(char *szSQL)
{
if(!m_bIsConnected)
{
printf("DB is disconnected!\n");
return 0;
}
try
{
m_pRset = m_pConn->Execute(szSQL, NULL, adCmdText);
}
catch(...)
{
return 0;
}
return m_pRset;
}
BOOL CADO::isStatus()
{
return m_bIsConnected;
}
Comments
Post a Comment