#include "stdafx.h"
#include <Windows.h>
#include "ADO.h"
CADO::CADO(void)
{
CoInitialize(NULL);
m_bTransactionError = false;
m_bTransaction = false;
m_lRecordCount = 0;
m_pConn.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_strDb = "127.0.0.1,1433";
m_strUser = "sa";
m_strPsw = "test";
}
CADO::~CADO(void)
{
Close();
m_pRecordset.Release();
m_pConn.Release();
CoUninitialize();
}
bool CADO::Close(void)
{
if (m_pConn == NULL || !IsOpen())
return false;
m_pConn->Close();
return true;
}
bool CADO::Connect(void)
{
string conData = "Provider=SQLOLEDB;Server=";
conData += m_strDb;
conData += ";uid=";
conData += m_strUser;
conData += ";pwd=";
conData += m_strPsw;
// conData += ";database=ASPlatConfigDB;";
try
{
HRESULT hRet = m_pConn->Open(conData.c_str(),"","",adConnectUnspecified);
if(!SUCCEEDED(hRet))
return false;
}
catch(_com_error &comError)
{
dump_com_error(comError);
return false;
}
return true;
}
bool CADO::IsOpen(void)
{
try
{
return (m_pConn != NULL && (m_pConn->State & adStateOpen));
}
catch(_com_error &e)
{
dump_com_error(e);
}
return false;
}
void CADO::dump_com_error(_com_error &e)
{
string ErrorStr;
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
ErrorStr = "MAdoDataBase Error=";
ErrorStr += "\n\tSource =";
ErrorStr += (LPCSTR)bstrSource;
ErrorStr += "\n\tDescripiion =";
ErrorStr += (LPCSTR)bstrDescription;
}
bool CADO::Execute(const string &exec)
{
if( m_pConn == NULL || exec.empty() || !IsOpen() )
return false;
try
{
if (m_bTransaction && m_bTransactionError)
return false;
m_pConn->Execute( exec.c_str(),NULL,adExecuteNoRecords );
return true;
}
catch(_com_error &e)
{
if (m_bTransaction)
{
m_bTransactionError = true;
}
dump_com_error(e);
}
return false;
}
_RecordsetPtr CADO::Query(const string &CommandStr, VARIANT * RecordsAffected /* = NULL */)
{
try
{
if (!IsOpen())
return NULL;
m_pRecordset = m_pConn->Execute(CommandStr.c_str(), RecordsAffected, adModeUnknown);
m_lRecordCount = GetRecordCount(m_pRecordset);
return m_pRecordset;
}
catch (_com_error& e)
{
dump_com_error(e);
return NULL;
}
}
long CADO::BeginTransaction(void)
{
if(m_pConn==NULL)
return -1;
try
{
m_bTransaction = true;
long lRet = m_pConn->BeginTrans();
m_bTransactionError = false;
return lRet;
}
catch(_com_error &e)
{
dump_com_error(e);
m_bTransactionError = true;
return -1;
}
return -1;
}
bool CADO::EndTransaction(void)
{
if(m_pConn == NULL)
return false;
try
{
bool bRet = false;
if (m_bTransactionError)
{
m_pConn->RollbackTrans();
bRet = false;
}
else
{
bRet = SUCCEEDED(m_pConn->CommitTrans());
}
m_bTransactionError = false;
m_bTransaction = false;
return bRet;
}
catch(_com_error &e)
{
dump_com_error(e);
return false;
}
return false;
}
long CADO::GetRecordCount(_RecordsetPtr set)
{
long nCount = 0;
if (!set->adoEOF)
{
set->MoveFirst();
while( !set->adoEOF )
{
++nCount;
set->MoveNext();
}
set->MoveFirst();
}
return nCount;
}
bool CADO::BeginWriteBlobData(const string& strSelectSQL)
{
if (m_pConn == NULL)
return false;
HRESULT hr = m_pRecordset->Open(strSelectSQL.c_str(), m_pConn.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
if (FAILED(hr))
return false;
if (!m_pRecordset->GetadoEOF())
m_pRecordset->MoveLast();
m_pRecordset->AddNew();
return true;
}
bool CADO::EndWriteBlobData()
{
if (m_pRecordset == NULL)
return false;
m_pRecordset->Update();
m_pRecordset->Close();
return true;
}
bool CADO::BeginReadBlobData(const string& strSelectSQL)
{
if (m_pConn == NULL)
return false;
m_pRecordset = m_pConn->Execute(strSelectSQL.c_str(), NULL, adCmdText);
if (m_pRecordset == NULL)
return false;
return true;
}
bool CADO::HasMoreBlobDataRecord() const
{
if (m_pRecordset == NULL)
return false;
return m_pRecordset->GetadoEOF() == VARIANT_FALSE;
}
void CADO::NextBlobDataRecord()
{
if (m_pRecordset == NULL)
return;
m_pRecordset->MoveNext();
}
bool CADO::EndReadBlobData()
{
if (m_pRecordset == NULL)
return false;
m_pRecordset->Close();
return true;
}
_variant_t CADO::GetCollect(const _variant_t& Index)
{
if (m_pRecordset == NULL)
return VT_EMPTY;
return m_pRecordset->GetCollect(Index);
}
bool CADO::GetCollect(_variant_t& vtValue, const _variant_t& Index)
{
if (m_pRecordset == NULL)
return false;
vtValue = m_pRecordset->GetCollect(Index);
return true;
}
bool CADO::GetBlobData(string& strBlobData, const _variant_t& Index)
{
if (m_pRecordset == NULL)
return false;
FieldPtr pItem = m_pRecordset->GetFields()->GetItem(Index);
if (pItem == NULL)
return false;
long lDataSize = pItem->ActualSize;
if (lDataSize <= 0)
return true;
_variant_t varBLOB;
varBLOB = pItem->GetChunk(lDataSize);
if(varBLOB.vt == (VT_ARRAY | VT_UI1))
{
char* pBuf = NULL;
SafeArrayAccessData(varBLOB.parray, (void **)&pBuf);
strBlobData.assign(pBuf, lDataSize);
SafeArrayUnaccessData(varBLOB.parray);
}
return true;
}
std::string CADO::GetBlobData(const _variant_t& Index)
{
string strBlobData;
GetBlobData(strBlobData, Index);
return strBlobData;
}
bool CADO::PutCollect(const _variant_t& Index, const _variant_t & pvar)
{
if (m_pRecordset == NULL)
return false;
m_pRecordset->PutCollect(Index, pvar);
return true;
}
bool CADO::PutBlobData(const _variant_t& Index, const string& strBlobData)
{
if (m_pRecordset == NULL)
return false;
SAFEARRAYBOUND rgs[1];
rgs[0].lLbound = 0;
rgs[0].cElements = strBlobData.size();
SAFEARRAY* pas = SafeArrayCreate(VT_UI1, 1, rgs);
if (pas == NULL)
return false;
char* pBuff = NULL;
SafeArrayAccessData(pas, (void **)&pBuff);
if (pBuff == NULL)
return false;
// 拷贝数据
strBlobData.copy(pBuff, strBlobData.size());
variant_t varBOLB;
varBOLB.vt = VT_ARRAY | VT_UI1;
varBOLB.parray = pas;
m_pRecordset->GetFields()->GetItem(Index)->AppendChunk(varBOLB);
SafeArrayUnaccessData(pas);
return true;
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页