c++自定义的数据库类

自定义的数据库类:Class DataBase 收藏
 class DataBase
{
private:
_ConnectionPtr m_pConnection;
_CommandPtr m_pCommand;
_RecordsetPtr m_res;
_variant_t rec_affected;
char m_cConstr[16];
char m_cUserName[32];
char m_cPassword[32];
bool m_bOpenType;
public:
DataBase();
~DataBase();
void REOpen();
bool IsOpen();
bool OpenDB( char *pContStr, char *pSbName, char *pSbPw );
bool OpenDB( char *pContStr );
bool GetData( char *pSqlStr, _RecordsetPtr &res );
bool Execute( char *pSqlStr );
bool NoneRetOpen( char *pSqlStr );
bool CommandPro( char *pProName );
_ConnectionPtr& GetConnection();
void CloseDB();
};

<< CPP >>
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","EndOfFile") rename("BOF","FirstOfFile")
#include <objbase.h>
#include "CDataBase.h"

DataBase::DataBase()
{
CoInitialize(NULL);
m_pConnection = NULL;
m_pCommand = NULL;
m_pConnection.CreateInstance(__uuidof(Connection));
m_res.CreateInstance(__uuidof(Recordset));
}

DataBase::~DataBase()
{
CloseDB();
if(m_res->GetState() == adStateOpen)
m_res->Close();
m_res.Release();
m_res = NULL;
m_pConnection.Release();
m_pConnection = NULL;
CoUninitialize();
}

_ConnectionPtr& DataBase::GetConnection()
{
return m_pConnection;
}

bool DataBase::OpenDB( char *pContStr, char *pSbName, char *pSbPw )
{
try
{
if(m_pConnection->State)
return TRUE;
// 打开本地Access库text.mdb
m_pConnection->Open( (_bstr_t)pContStr, pSbName, pSbPw, adModeUnknown );
if( m_pConnection->State != adStateOpen )
return FALSE;
m_pConnection->put_ConnectionTimeout(long(10));//设置超时
m_pConnection->put_CommandTimeout(long(25));

m_pCommand.CreateInstance(__uuidof(Command));
m_pCommand->ActiveConnection = m_pConnection;
strcpy( m_cConstr, pContStr );
strcpy( m_cUserName, pSbName );
strcpy( m_cPassword, pSbPw );

m_bOpenType = TRUE;
}
catch(_com_error e)
{
return FALSE;
}
return TRUE;
}


bool DataBase::OpenDB( char *pContStr )
{
try
{
if(m_pConnection->State)
return TRUE;

m_pConnection->ConnectionString="File Name=.\\DBConn.udl";
m_pConnection->PutConnectionTimeout(10);
m_pConnection->PutCommandTimeout(25);

m_pConnection->Open("","","",NULL);

if( m_pConnection->State != adStateOpen )
return FALSE;

m_bOpenType = FALSE;
m_pCommand.CreateInstance(__uuidof(Command));
m_pCommand->ActiveConnection = m_pConnection;
strcpy( m_cConstr, pContStr );
}
catch(_com_error e)
{
return FALSE;
}
return TRUE;
}

VOID DataBase::CloseDB()
{
if(m_pConnection==NULL) return;
if(IsOpen())
m_pConnection->Close();
}

bool DataBase::Execute( char *pSqlStr )
{
if(!IsOpen()) return FALSE;
try
{
m_pConnection->Execute( pSqlStr,&rec_affected,0 );
}
catch(_com_error e)
{
return FALSE;
}
return TRUE;
}

bool DataBase::NoneRetOpen( char *pSqlStr )
{
if(!IsOpen()) return FALSE;

if(m_res->GetState() == adStateOpen)
m_res->Close();
try
{
m_res->Open(pSqlStr,m_pConnection.GetInterfacePtr(),adOpenForwardOnly ,adLockReadOnly,adCmdText);
}
catch(_com_error e)
{
return FALSE;
}
return TRUE;
}

bool DataBase::CommandPro( char *pProName )
{
if(!IsOpen()) return FALSE;
try
{
m_pCommand->CommandText = pProName;
m_pCommand->Execute(NULL,NULL, adCmdText);
}
catch(_com_error *e)
{
MessageBox( NULL, e->ErrorMessage(), "", MB_OK );
return FALSE;
}
return TRUE;
}
/*
bool DataBase::CommandPro( char *pProName )
{
if(!IsOpen()) return FALSE;
try
{
m_pCommand->CommandText = pProName;
m_pCommand->Execute(NULL,NULL, adCmdText);
}
catch(_com_error *e)
{
MessageBox( NULL, e->ErrorMessage(), "", MB_OK );
return FALSE;
}
return TRUE;
}
*/

bool DataBase::IsOpen()
{
try
{
return (m_pConnection != NULL && (m_pConnection->State & adStateOpen));
}
catch(_com_error e)
{
return FALSE;
}
}

void DataBase::REOpen()
{
try
{
if (m_pConnection->State != adStateOpen)
m_pConnection->Open( m_cConstr, m_cUserName, m_cPassword, adModeUnknown);
}
catch(_com_error e)
{
}
}

bool DataBase::GetData( char *pSqlStr ,_RecordsetPtr &res )
{
if(!IsOpen()) return FALSE;

if(res->GetState() == adStateOpen)
res->Close();
try
{
res->Open(pSqlStr,m_pConnection.GetInterfacePtr(),adOpenForwardOnly ,adLockReadOnly,adCmdText);
}
catch(_com_error e)
{
return FALSE;
}
return TRUE;
}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/johnny_83/archive/2007/07/22/1701801.aspx

你可能感兴趣的:(C++)