自己简单封装的ado.路径那个还没改。。
使用方法如下
#include "MFCADO.h"
void test_query(CMFCADO& c)
{
if(c.GetRecordSet("select * from reward"))
{
while( RS_NOT_EOF(c.m_pRS) )
{
printf("%s %d \n ", RS_GET_STR(c.m_pRS,"name") , RS_GET_INT(c.m_pRS,"money") );
RS_MOVE_NEXT(c.m_pRS);
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
CMFCADO c;
if(c.OpenConn("")){
printf("OpenConn ok\n");
}
//c.ExecuteSQL("insert into reward (name,money)values('xx',10000)");
while(1)
{
test_query(c);
}
Sleep(1000);
return 0;
}
CMFCADO.h
#pragma once
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF", "adoBOF")
#define RS_NOT_EOF(rs) !rs->adoEOF
#define RS_GET_STR(rs,colname) (char*)(_bstr_t)rs->GetCollect(colname)
#define RS_GET_INT(rs,colname) atoi((char*)(_bstr_t)rs->GetCollect(colname))
#define RS_MOVE_NEXT(rs) rs->MoveNext();
/************************************************************************/
/*
mfc ado
CMFCADO c;
if(c.OpenConn("")){
printf("OpenConn ok");
}
c.ExecuteSQL("insert into table (x,b)values(7,10000)");
simawei@qq.com
2012.8.1
*/
/************************************************************************/
class CMFCADO
{
public:
CMFCADO(void);
~CMFCADO(void);
//通过文件路径打开mdb数据库
bool OpenConn(LPCTSTR mdb_filepath);
void CloseConn();
bool CMFCADO::GetRecordSet(LPCTSTR bstrSQL);
bool CMFCADO::ExecuteSQL(LPCTSTR bstrSQL);
void CloseRecordset(void);
_RecordsetPtr m_pRS;//添加一个指向RecordSet对象的指针
private:
_ConnectionPtr m_pConnection;//添加一个指向Connection对象的指针
};
CMFCADO.cpp
#include "StdAfx.h"
#include ".\mfcado.h"
CMFCADO::CMFCADO(void)
{
::CoInitialize(NULL);//初始化OLE/COM库环境
}
CMFCADO::~CMFCADO(void)
{
CloseConn();
::CoUninitialize();
}
void CMFCADO::CloseConn()
{
try
{
if ( m_pConnection!=NULL )
{
m_pConnection->Close();
m_pConnection=NULL;
}
}
catch(_com_error &e)
{
//printf("CADO::Close() exception: %s\n",(char*)e.Description());//显示错误信息
}
}
bool CMFCADO::OpenConn(LPCTSTR mdb_filepath)
{
//m_pConnection.CreateInstance(_uuidof(Connection));
//创建Connection 对象
m_pConnection.CreateInstance("ADODB.Connection");
try
{
HRESULT hr=m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=x.mdb","","",adModeUnknown);
if (FAILED(hr))
{
m_pConnection=NULL;
printf("Connection->Open FAILED\n");
}
}
catch(_com_error e)
{
//AfxMessageBox( (char*)e.Description() );//显示错误信息);
printf("[%s] err [%s]\n",__FUNCTION__,(char*)e.Description());
return false;
}
return true;
}
bool CMFCADO::ExecuteSQL(LPCTSTR bstrSQL)
{
_variant_t vRowsAffected;
try
{
m_pConnection->Execute(bstrSQL,&vRowsAffected,adCmdText);
//vRowsAffected.intVal;
}
catch (_com_error e)
{
printf("[%s] err [%s]\n",__FUNCTION__,(char*)e.Description());
return false;
}
return true;
}
void CMFCADO::CloseRecordset(void)
{
if(m_pRS !=NULL )
{
m_pRS->Close();
m_pRS=NULL;
}
}
bool CMFCADO::GetRecordSet(LPCTSTR bstrSQL)
{
CloseRecordset();
//Log(MSG_SQL,"Query [%s]\n",bstrSQL);
//m_pRecordset.CreateInstance(__uuidof(Recordset));
//创建记录集对象
m_pRS.CreateInstance("ADODB.Recordset");
try
{
/*
lockType 表示当打开记录集时,数据提供者用于锁定数据库的类型:
adLockReadOnly 数据不能改变,这是缺省值!
adLockPessimistic 数据提供者在开始编辑数据的时候锁定记录
adLockOptimistic 仅当调用update方法时,数据提供者锁定记录
adLockBatchOptimistic 用于批处理修改
他们的常量值定义分别是:
adLockReadOnly = 1
adLockPessimistic = 2
adLockOptimistic = 3
adLockBatchOptimistic = 4*/
//取得表中的记录
HRESULT hr= m_pRS->Open(bstrSQL,
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针 ,
adOpenDynamic,
adLockReadOnly, //,adLockBatchOptimistic adLockOptimistic
adCmdText);
if (FAILED(hr))
{
m_pRS=NULL;
}else{
}
}
catch (_com_error e)
{
printf("[%s] err [%s]\n",__FUNCTION__,(char*)e.Description());
return false;
}
return true;
}