MS2010 VC++使用ADO访问数据库

下文介绍了使用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;
}
/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lintaoshen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值