下文介绍了使用MS2010开发坏境中的VC++和Microsoft.Jet.OLEDB.4.0数据库引擎操作ACCESS的通用类
1.头文件OperatorADO.h
#pragma once
#include <atlbase.h>
#include <afxoledb.h>
#include <atlplus.h>
#include <atldbsch.h>
#include <atlconv.h>
#include <atlcoll.h>
typedef struct CSchemaTableInfo
{
CString csSchema;
CString csName;
CSchemaTableInfo()
{
}
CSchemaTableInfo( const CSchemaTableInfo& other )
{
csSchema = other.csSchema;
csName = other.csName;
}
CSchemaTableInfo& operator=( const CSchemaTableInfo& other )
{
if( &other != this )
{
csSchema = other.csSchema;
csName = other.csName;
}
return *this;
}
};
class OperatorADO
{
public:
OperatorADO(void);
~OperatorADO(void);
// 根据连接字符串获得数据源类实例
BOOL OpenDataSource(CString strConn,CDataSource* pDS,CSession* pSession);
//
// 访问数据集的方式
//
//CDynamicAccessor形式访问数据
// 通过SQL语句获得记录集
BOOL GetRecordsFromSQL(CSession* pSession,CCommand<CDynamicAccessor>* pRs, CString strSQL);
// 从记录集中获得数据
//template<class T> //CCommand<CDynamicAccessor>
void* GetDataFromRecords(CCommand<CDynamicAccessor>* pRs,int nRow,int nCol,DBTYPE &type);
//CDynamicStringAccessor形式访问数据
// 通过SQL语句获得记录集
BOOL GetRecordsFromSQL(CSession* pSession,CCommand<CDynamicStringAccessor>* pRs, CString strSQL);
// 从记录集中获得数据
//template<class T> //CCommand<CDynamicAccessor>
CHAR* GetDataFromRecords(CCommand<CDynamicStringAccessor>* pRs,int nRow,int nCol,DBTYPE &type);
//CXMLAccessor形式访问数据
// 通过SQL语句获得记录集
BOOL GetRecordsFromSQL(CSession* pSession,CCommand<CXMLAccessor>* pRs, CString strSQL);
// 从记录集中获得数据
//template<class T> //CCommand<CDynamicAccessor>
CString GetDataFromRecords(CCommand<CXMLAccessor>* pRs,int nRow,int nCol,DBTYPE &type);
//
//16进制数转为ASCII码
static void d2h( BYTE n, char* pszHex );
void* GetValue(void);
void* GetSubValue(void);
// 得到数据库中数据表的信息
BOOL GetTableInfo(CSession* pSession, CAtlArray<CSchemaTableInfo>* pArray);
};
2.实现文件OperatorADO.cpp
#include "StdAfx.h"
#include "OperatorADO.h"
#define MAX_DATALEN 1000000
OperatorADO::OperatorADO(void)
{
::CoInitialize(NULL);
}
OperatorADO::~OperatorADO(void)
{
::CoUninitialize();
}
// 根据连接字符串获得数据源类实例
BOOL OperatorADO::OpenDataSource(CString strConn,CDataSource* pDS,CSession* pSession)
{
if(pDS==NULL||pSession==NULL)
return FALSE;
HRESULT hr=pDS->OpenFromInitializationString(CT2COLE(strConn));
if(FAILED(hr))
return FALSE;
hr = pSession->Open( *pDS );
if(FAILED(hr))
{
pDS->Close();
return FALSE;
}
return TRUE;
}
// 通过SQL语句获得记录集
BOOL OperatorADO::GetRecordsFromSQL(CSession* pSession,CCommand<CDynamicAccessor>* pRs, CString strSQL)
{
if(pSession==NULL||pRs==NULL)
return FALSE;
int nBlobDeal=1;
switch( nBlobDeal )
{
case 1:
pRs->SetBlobHandling( DBBLOBHANDLING_DEFAULT );
break;
case 2:
pRs->SetBlobHandling( DBBLOBHANDLING_NOSTREAMS );
break;
case 3:
pRs->SetBlobHandling( DBBLOBHANDLING_SKIP );
break;
}
CDBPropSet pset( DBPROPSET_ROWSET );
pset.AddProperty( DBPROP_ISequentialStream, true, DBPROPOPTIONS_OPTIONAL );
pset.AddProperty( DBPROP_IStream, true, DBPROPOPTIONS_OPTIONAL );
pset.AddProperty( DBPROP_CANFETCHBACKWARDS, true, DBPROPOPTIONS_OPTIONAL );
pset.AddProperty( DBPROP_CANSCROLLBACKWARDS, true, DBPROPOPTIONS_OPTIONAL );
HRESULT hr = pRs->Open(*pSession, strSQL, &pset );
if( FAILED( hr ) )
{
return FALSE;
}
return TRUE;
}
/