#include "stdafx.h"
#include "adoDatabase.h"
#define ChunkSize 100
BOOL CJetEngine::CCompactDatabase(CString strDatabaseSource, CString strDatabaseDestination)
{
try
{
::CoInitialize(NULL);
IJetEnginePtr jet(__uuidof(JetEngine));
HRESULT hr = jet->CompactDatabase(_bstr_t(strDatabaseSource.GetBuffer(0)), _bstr_t(strDatabaseDestination.GetBuffer(0)));
::CoUninitialize();
return hr == S_OK;
}
catch(_com_error & e)
{
::CoUninitialize();
return FALSE;
}
}
///////////////////////////////////////////////////////
//
// CADODatabase Class
//
DWORD CADODatabase::GetRecordCount(_RecordsetPtr m_pRs)
{
DWORD numRows = 0;
numRows = m_pRs->GetRecordCount();
if(numRows == -1)
{
if(m_pRs->EndOfFile != VARIANT_TRUE)
m_pRs->MoveFirst();
while(m_pRs->EndOfFile != VARIANT_TRUE)
{
numRows++;
m_pRs->MoveNext();
}
if(numRows > 0)
m_pRs->MoveFirst();
}
return numRows;
}
BOOL CADODatabase::Open(LPCTSTR lpstrConnection, LPCTSTR lpstrUserID, LPCTSTR lpstrPassword)
{
HRESULT hr = S_OK;
if(IsOpen())
Close();
if(strcmp(lpstrConnection, _T("")) != 0)
m_strConnection = lpstrConnection;
ASSERT(!m_strConnection.IsEmpty());
try
{
if(m_nConnectionTimeout != 0)
m_pConnection->PutConnectionTimeout(m_nConnectionTimeout);
//
CString strUserID = lpstrUserID;
CString strPassWord = lpstrPassword;
BSTR connectionTemp = m_strConnection.AllocSysString();
BSTR userTemp = strUserID.AllocSysString();
BSTR pwdTemp = strPassWord.AllocSysString();
_bstr_t strConnect(connectionTemp,false);
_bstr_t strUser(userTemp,false);
_bstr_t strPwd(pwdTemp,false);
hr = m_pConnection->Open(strConnect, strUser,strPwd, NULL);
::SysFreeString(connectionTemp);
::SysFreeString(userTemp);
::SysFreeString(pwdTemp);
//
//hr = m_pConnection->Open(_bstr_t(m_strConnection), _bstr_t(lpstrUserID), _bstr_t(lpstrPassword), NULL);
return hr == S_OK;
}
catch(_com_error &e)
{
//dump_com_error(e);
return FALSE;
}
}
void CADODatabase::dump_com_error(_com_error &e)
{
CString ErrorStr;
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
ErrorStr.Format( "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_strErrorDescription = (LPCSTR)bstrDescription ;
m_strLastError = _T("Connection String = " + GetConnectionString() + '\n' + ErrorStr);
m_dwLastError = e.Error();
#ifdef _DEBUG
AfxMessageBox(ErrorStr, MB_OK | MB_ICONERROR );
#endif
}
BOOL CADODatabase::IsOpen()
{
if(m_pConnection )
return m_pConnection->GetState() != adStateClosed;
return FALSE;
}
void CADODatabase::Close()
{
if(IsOpen())
m_pConnection->Close();
}
///////////////////////////////////////////////////////
//
// CADORecordset Class
//
CADORecordset::CADORecordset()
{
m_pRecordset = NULL;
m_pCmd = NULL;
m_strQuery = _T("");
m_strLastError = _T("");
m_dwLastError = 0;
m_pRecBinding = NULL;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pCmd.CreateInstance(__uuidof(Command));
m_nEditStatus = CADORecordset::dbEditNone;
m_nSearchDirection = CADORecordset::searchForward;
}
CADORecordset::CADORecordset(CADODatabase* pAdoDatabase)
{
m_pRecordset = NULL;
m_pCmd = NULL;
m_strQuery = _T("");
m_strLastError = _T("");
m_dwLastError = 0;
m_pRecBinding = NULL;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pCmd.CreateInstance(__uuidof(Command));
m_nEditStatus = CADORecordset::dbEditNone;
m_nSearchDirection = CADORecordset::searchForward;
m_pConnection = pAdoDatabase->GetActiveConnection();
}
BOOL CADORecordset::Open(_ConnectionPtr mpdb, LPCTSTR lpstrExec, int nOption)
{
Close();
if(strcmp(lpstrExec, _T("")) != 0)
m_strQuery = lpstrExec;
ASSERT(!m_strQuery.IsEmpty());
if(m_pConnection == NULL)
m_pConnection = mpdb;
m_strQuery.TrimLeft();
BOOL bIsSelect = m_strQuery.Mid(0, strlen("Select ")).CompareNoCase("select ") == 0 && nOption == openUnknown;
try
{
m_pRecordset->CursorType = adOpenStatic;
m_pRecordset->CursorLocation = adUseClient;
if(bIsSelect || nOption == openQuery || nOption == openUnknown)
m_pRecordset->Open((LPCSTR)m_strQuery, _variant_t((IDispatch*)mpdb, TRUE),
adOpenStatic, adLockOptimistic, adCmdUnknown);
else if(nOption == openTable)
m_pRecordset->Open((LPCSTR)m_strQuery, _variant_t((IDispatch*)mpdb, TRUE),
adOpenKeyset, adLockOptimistic, adCmdTable);
else if(nOption == openStoredProc)
{
m_pCmd->ActiveConnection = mpdb;
m_pCmd->CommandText = _bstr_t(m_strQuery);
m_pCmd->CommandType = adCmdStoredProc;
m_pConnection->CursorLocation = adUseClient;
m_pRecordset = m_pCmd->Execute(NULL, NULL, adCmdText);
}
else
{
TRACE( "Unknown parameter. %d", nOption);
return FALSE;
}
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
return m_pRecordset != NULL;
}
BOOL CADORecordset::Open(LPCTSTR lpstrExec, int nOption)
{
ASSERT(m_pConnection != NULL);
ASSERT(m_pConnection->GetState() != adStateClosed);
return Open(m_pConnection, lpstrExec, nOption);
}
BOOL CADORecordset::OpenSchema(int nSchema, LPCTSTR SchemaID)
{
try
{
_variant_t vtSchemaID = vtMissing;
if(strlen(SchemaID) != 0)
vtSchemaID = SchemaID;
m_pRecordset = m_pConnection->OpenSchema((enum SchemaEnum)nSchema, vtMissing, vtSchemaID);
return TRUE;
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
BOOL CADORecordset::Requery()
{
if(IsOpen())
{
try
{
//m_pRecordset->Requery(adExecuteRecord);
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
return TRUE;
}
BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, double& dbValue)
{
double val = (double)NULL;
_variant_t vtFld;
try
{
vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
switch(vtFld.vt)
{
case VT_R4:
val = vtFld.fltVal;
break;
case VT_R8:
val = vtFld.dblVal;
break;
case VT_DECIMAL:
//Corrected by Jos?Carlos Mart�nez Gal�n
val = vtFld.decVal.Lo32;
val *= (vtFld.decVal.sign == 128)? -1 : 1;
val /= pow(10.0, vtFld.decVal.scale);
break;
case VT_UI1:
val = vtFld.iVal;
break;
case VT_I2:
case VT_I4:
val = vtFld.lVal;
break;
case VT_INT:
val = vtFld.intVal;
break;
case VT_NULL:
case VT_EMPTY:
val = 0;
break;
default:
val = vtFld.dblVal;
}
dbValue = val;
return TRUE;
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
BOOL CADORecordset::GetFieldValue(int nIndex, double& dbValue)
{
double val = (double)NULL;
_variant_t vtFld;
_variant_t vtIndex;
vtIndex.vt = VT_I2;
vtIndex.iVal = nIndex;
try
{
vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
switch(vtFld.vt)
{
case VT_R4:
val = vtFld.fltVal;
break;
case VT_R8:
val = vtFld.dblVal;
break;
case VT_DECIMAL:
//Corrected by Jos?Carlos Mart�nez Gal�n
val = vtFld.decVal.Lo32;
val *= (vtFld.decVal.sign == 128)? -1 : 1;
val /= pow(10.0, vtFld.decVal.scale);
break;
case VT_UI1:
val = vtFld.iVal;
break;
case VT_I2:
case VT_I4:
val = vtFld.lVal;
break;
case VT_INT:
val = vtFld.intVal;
break;
case VT_NULL:
case VT_EMPTY:
val = 0;
break;
default:
val = 0;
}
dbValue = val;
return TRUE;
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, long& lValue)
{
long val = (long)NULL;
_variant_t vtFld;
try
{
vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
if(vtFld.vt !
- 1
- 2
- 3
前往页