📄 adodatabase.cpp
字号:
// ADODatabase.cpp: implementation of the CADODatabase class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ADODatabase.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CADODatabase::CADODatabase()
{
::CoInitialize(NULL);
m_pConnection = NULL;
m_strConnection = _T("");
m_pConnection.CreateInstance(__uuidof(Connection));
}
CADODatabase::~CADODatabase()
{
Close();
m_pConnection.Release();
m_pConnection = NULL;
::CoUninitialize();
}
long CADODatabase::BeginTransaction()
{
return m_pConnection->BeginTrans();
}
void CADODatabase::Close()
{
if(IsOpen())
m_pConnection->Close();
}
long CADODatabase::CommitTransaction()
{
return m_pConnection->CommitTrans();
}
void CADODatabase::dump_com_error(_com_error &e)
{
CString ErrorStr;
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
ErrorStr.Format(_T("CADODataBase Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n"),
e.Error(), e.ErrorMessage(), (LPCSTR)bstrSource, (LPCSTR)bstrDescription );
m_strLastError = _T("Connection String = " + GetConnectionString() + '\n' + ErrorStr);
#ifdef _DEBUG
AfxMessageBox( ErrorStr, MB_OK | MB_ICONERROR );
#endif
}
BOOL CADODatabase::Execute(LPCTSTR lpstrExec)
{
ASSERT(m_pConnection != NULL);
ASSERT(strcmp(lpstrExec, _T("")) != 0);
try
{
m_pConnection->Execute(_bstr_t(lpstrExec), NULL, adExecuteNoRecords);
}
catch(_com_error &e)
{
dump_com_error(e);
}
return true;
}
_ConnectionPtr CADODatabase::GetActiveConnection()
{
return m_pConnection;
}
CString CADODatabase::GetConnectionString()
{
return m_strConnection;
}
CString CADODatabase::GetLastError()
{
return m_strLastError;
}
DWORD CADODatabase::GetRecordCount(_RecordsetPtr m_pRs)
{
DWORD numRows = 0;
numRows = m_pRs->GetRecordCount();
if(numRows == -1)
{
if(m_pRs->adoEOF!= VARIANT_TRUE)
m_pRs->MoveFirst();
while(m_pRs->adoEOF != VARIANT_TRUE)
{
numRows++;
m_pRs->MoveNext();
}
if(numRows > 0)
m_pRs->MoveFirst();
}
return numRows;
}
BOOL CADODatabase::IsOpen()
{
if(m_pConnection)
return m_pConnection->GetState() != adStateClosed;
return false;
}
int CADODatabase::Open(CString lpstrConnection)
{
HRESULT hr = S_OK;
if(IsOpen())
Close();
if(! lpstrConnection.IsEmpty())
m_strConnection = lpstrConnection;
ASSERT(!m_strConnection.IsEmpty());
try
{
hr = m_pConnection->Open(_bstr_t(m_strConnection), "", "", NULL);
return hr == S_OK;
}
catch(_com_error &e)
{
dump_com_error(e);
}
return false;
}
long CADODatabase::RollbackTransaction()
{
return m_pConnection->RollbackTrans();
}
void CADODatabase::SetConnectionString(LPCTSTR lpstrConnection)
{
m_strConnection = lpstrConnection;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -