//
// MODULE: Ado2.cpp
//
// AUTHOR: Carlos Antollini <cantollini@hotmail.com>
//
// Copyright (c) 2001-2004. All Rights Reserved.
//
// Date: August 01, 2005
//
// Version 2.20
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name and all copyright
// notices remains intact.
//
// An email letting me know how you are using it would be nice as well.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage/loss of business that
// this product may cause.
//
//
//////////////////////////////////////////////////////////////////////
#include "ado2.h"
#define ChunkSize 100
///////////////////////////////////////////////////////
//
// CJetEngine Class
//
BOOL CJetEngine::CompactDatabase(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)));
jet.Release();
::CoUninitialize();
return hr == S_OK;
}
catch(_com_error)
{
::CoUninitialize();
return FALSE;
}
}
BOOL CJetEngine::RefreshCache(ADODB::_Connection *pconn)
{
//Added by doodle@email.it
try
{
::CoInitialize(NULL);
IJetEnginePtr jet(__uuidof(JetEngine));
HRESULT hr = jet->RefreshCache(pconn);
::CoUninitialize();
return hr == S_OK;
}
catch(_com_error)
{
::CoUninitialize();
return FALSE;
}
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);
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
throw CADOException(e.Error(), e.Description());
}
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 && m_pRecordset->GetState()!= adStateClosed;
}
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 /*= _T("")*/)
{
try
{
_variant_t vtSchemaID = vtMissing;
if(strlen(SchemaID) != 0)
{
vtSchemaID = SchemaID;
nSchema = adSchemaProviderSpecific;
}
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, 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_CY: //Added by John Andy Johnson!!!!
vtFld.ChangeType(VT_R8);
val = vtFld.dblVal;
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.
DataBase application class ado


标题中的"DataBase application class ado"指的是使用ADO(ActiveX Data Objects)来构建数据库应用程序的类。ADO是微软提供的一种数据库访问技术,它允许开发者通过编程语言与各种数据库系统进行交互,如Microsoft SQL Server、Oracle、MySQL等。ADO是OLE DB的一部分,而OLE DB是一个组件对象模型(COM),用于访问数据存储。 在描述中提到的"通用数据库存取接口,包括查询、修改、删除等",这是ADO的核心功能。通过ADO,开发者可以执行以下操作: 1. **查询**:使用SQL语句或命令对象(Command)执行SELECT语句,获取数据并将其填充到Recordset对象中,以便于遍历和处理。 2. **修改**:通过Recordset对象,可以更新、插入和删除记录。例如,移动记录指针到特定位置,更改字段值,然后调用Update方法保存更改。 3. **删除**:同样通过Recordset,可以标记记录为删除,并在合适的时候调用Delete方法。 现在我们来看看压缩包中的文件名,它们可能是实现这些功能的源代码文件: - `ado2.cpp`:这是一个使用C++编写的ADO代码实现,可能包含了ADO接口的封装和数据库操作的具体实现。 - `oledb2.cpp`:OLE DB是更底层的数据访问接口,而这个文件可能包含了使用OLE DB接口的代码,可能是为了在某些特定情况下获得更高效或更细粒度的控制。 - `ado2.h`:这是头文件,定义了ADO相关的类、结构和函数声明,供`ado2.cpp`和其他使用ADO的源文件包含和使用。 - `oledb2.h`:同理,这个头文件可能包含了OLE DB相关的接口声明。 在实际应用中,`ado2.cpp`和`ado2.h`可能定义了一个封装了ADO接口的类,比如DBAccess,提供简单的API供其他模块调用,进行数据库操作。而`oledb2.cpp`和`oledb2.h`可能提供了更底层的接口,用于处理更复杂的需求或者优化性能。 这些文件组合起来构成了一个使用ADO和OLE DB进行数据库操作的C++应用程序框架。通过这个框架,开发者可以方便地实现对数据库的查询、修改和删除操作,而无需关心底层的数据库连接细节和数据传输机制。这极大地提高了开发效率,使得代码更加模块化和可维护。
































- 1

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整

- 粉丝: 0
- 资源: 1
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 基于Java语言的CMS看点资讯项目设计源码
- 基于Springboot+Mybatis框架的JavaWeb员工管理系统后端设计源码
- 基于C++和C语言的文件软硬件加解密平台设计源码
- gf-golang资源
- liqin-songs-春节主题资源
- DeepSeek经验分享-DeepSeek资源
- 活动发布管理-活动资源
- QL6502-汇编语言资源
- 基于C语言的串口助手硬件资料设计源码
- 基于Scala与前端技术栈的空气质量分析与可视化系统设计源码
- 基于Vue3+Electron的医师电子化注册信息系统单机版设计源码
- 基于蒙特卡洛模拟与启发式同步回带削减的MATLAB实现:风光氢负荷场景生成与高效缩减,MATLAB下的风光氢场景生成与启发式同步回带削减法研究,MATLAB代码:风光氢的场景生成与缩减 关键词:风电
- 基于24小时容量利用系数法的线损分摊方法及其通用程序应用:matlab与python版解读与应用实例 ,基于24小时容量利用系数法的线损分摊程序:通用解决方案与Matlab及Python实现,24容量
- 基于C++语言的跨平台Frame框架设计源码
- COMSOL 5.6版超声仿真:检测焊缝缺陷模型专版应用,版本兼容性说明及优化升级解析,COMSOL 5.6超声仿真检测焊缝缺陷新功能解析,COMSOL超声仿真:检测焊缝缺陷 版本为5.6,低于5.6
- 小程序商城源码-Java-C语言资源


