需要俩个类一起用。第一个类 RxRecordset 如下:
。H文件
#if !defined(AFX_RXRECORDSET_H__B364FBA6_9899_49DC_BAD0_CC99F6F5D31B__INCLUDED_)
#define AFX_RXRECORDSET_H__B364FBA6_9899_49DC_BAD0_CC99F6F5D31B__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class RxRecordset
{
public:
RxRecordset();
virtual ~RxRecordset();
CString GetFieldType(int nNumber);
CString GetSql();
CString GetFieldName(int nNumber);
void Close();
void GetErrors(_com_error eErrors);
_RecordsetPtr GetRecordset();
int GetFieldCount();
int GetRecordCount();
bool Move(int nRecordNumber);
bool MovePrevious();
bool MoveNext();
bool MoveLast();
bool MoveFirst();
bool Open(CString srecordset, UINT adCmd=adCmdTable);
CString GetFieldValue(CString Field);
private:
CString sRecordset;
_RecordsetPtr rst;
};
#endif
。CPP文件
#include "stdafx.h"
#include "PFA.h"
#include "RxRecordset.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
RxRecordset::RxRecordset()
{
}
RxRecordset::~RxRecordset()
{
}
CString RxRecordset::GetFieldValue(CString Field)
{
CString sValue;
_variant_t value;
value=rst->GetCollect((_bstr_t)Field);
if(value.vt==VT_EMPTY ||value.vt==VT_NULL)
sValue="";
else
{
sValue=(char*)(_bstr_t)value;
sValue.TrimRight();//去空格的功能
sValue.TrimLeft();
}
return sValue;
}
bool RxRecordset::Open(CString srecordset, UINT adCmd)
{
//CPFAApp cnn; //这局部变量怎么能GetConn()呢
//CPFAApp* pApp = (CPFAApp*)AfxGetApp();//AfxGetApp返回一个CWinApp对象指针
//就是MFC生成的C??App.cpp中定义的那个对象
if(adCmd==adCmdTable) //要真正得到对象的地址
sRecordset=srecordset;
else
sRecordset="("+srecordset+") DERIVEDTBL";
try
{
// rst=(((CPFAApp*)AfxGetApp())->GetConn())->Execute((_bstr_t)srecordset,NULL,adCmd);
rst=m_pCon->Execute((_bstr_t)srecordset,NULL,adCmd);
}
catch(_com_error&e)
{
this->GetErrors(e);//this代表当前的RxRecordset实例
return FALSE;
}
return TRUE;
}
int RxRecordset::GetRecordCount()
{
int nCount=0;
try
{
rst->MoveFirst();
}
catch(...)
{
return 0;
}
if(rst->adoEOF)
return 0;
while(!rst->adoEOF)
{
rst->MoveNext();
nCount=nCount+1;
}
rst->MoveFirst();
return nCount;
}
bool RxRecordset::MoveFirst()
{
try
{
rst->MoveFirst();
}
catch(_com_error &e)
{
this->GetErrors(e);
return FALSE;
}
return TRUE;
}
bool RxRecordset::MoveNext()
{
try
{
rst->MoveNext();
}
catch(_com_error&e)
{
this->GetErrors(e);
return FALSE;
}
return TRUE;
}
bool RxRecordset::MoveLast()
{
try
{
rst->MoveLast();
}
catch(_com_error&e)
{
this->GetErrors(e);
return FALSE;
}
return TRUE;
}
bool RxRecordset::MovePrevious()
{
try
{
rst->MovePrevious();
}
catch(_com_error&e)
{
this->GetErrors(e);
return FALSE;
}
return TRUE;
}
bool RxRecordset::Move(int nRecordNumber)
{
try
{
rst->MoveFirst();
rst->Move(nRecordNumber);
}
catch(_com_error &e)
{
this->GetErrors(e);
return FALSE;
}
return TRUE;
}
int RxRecordset::GetFieldCount()
{
int count;
try
{
count=rst->GetFields()->GetCount();
}
catch(...)
{
return -1;
}
return count;
}
_RecordsetPtr RxRecordset::GetRecordset()
{
return rst;
}
void RxRecordset::GetErrors(_com_error eErrors)
{
CString string;
ErrorsPtr pErrors=m_pCon->GetErrors();//chucuo
if (pErrors->GetCount()==0)
{
string=(char*)(_bstr_t)eErrors.ErrorMessage();
AfxMessageBox(string);
}
else
{
for (int i=0;i<pErrors->GetCount();i++)
{
_bstr_t desc=pErrors->GetItem((long)i)->GetDescription();
string=(char*)desc;
AfxMessageBox(string);
}
}
}
void RxRecordset::Close()
{
rst->Close();
}
CString RxRecordset::GetFieldName(int nNumber)
{
CString sName;
_variant_t vName;
try
{
vName=rst->GetFields()->GetItem((long)nNumber)->GetName();
}
catch(_com_error &e)
{
GetErrors(e);
return "";
}
if(vName.vt==VT_EMPTY)
return "";
sName=(char*)(_bstr_t)vName;
sName.TrimLeft();
sName.TrimRight();
return sName;
}
CString RxRecordset::GetSql()
{
return sRecordset;
}
第二个类 ADOConn 如下:
。H文件
#if !defined(AFX_ADOCONN_H__0CD30419_D230_4F42_BBCC_E118D4238231__INCLUDED_)
#define AFX_ADOCONN_H__0CD30419_D230_4F42_BBCC_E118D4238231__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "RxRecordset.h"
class ADOConn
{
public:
ADOConn();
virtual ~ADOConn();
void OnInitADOConn();//初始化连接数据库
//执行查询
_RecordsetPtr& GetRecordset(_bstr_t bstrSQL);
_ConnectionPtr GetConnection();
//执行SQL语句,Insert Update _variant_t
BOOL ExecuteSQL(_bstr_t bstrSQL);
void ExitConnect();
CString AutoNumber(CString sTable, CString sFieldName, CString sCode,int Style);
int GetRecordCount(_RecordsetPtr pRst);
private:
_ConnectionPtr m_pCon;
_RecordsetPtr m_pRs;
};
#endif
。CPP文件:
#include "stdafx.h"
#include "ADOConn.h"
#include "MainFrm.h"
#include "AppFullPath.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Construction/Destruction
//
ADOConn::ADOConn()
{
}
ADOConn::~ADOConn()
{
}
void ADOConn::OnInitADOConn()
{
::CoInitialize(NULL);
_bstr_t bstrConnect;
CString strConnect;
char m_pzUser[128]; //登录数据库用户
char m_pzPassWord[128]; //登录数据库密码
char m_pzDataBase[128]; //登录数据库名称
char m_pzServer[128]; //服务器名称
CAppFullPath dlgpath;
CFileFind finder;
BOOL finded;
CString databasepath;
databasepath=dlgpath.GetDatabasePath();
finded=finder.FindFile(databasepath);
HRESULT hr;
try
{
m_pCon.CreateInstance(__uuidof(Connection));
m_pCon->CursorLocation =adUseClient;
if(!finded)
{
AfxMessageBox("数据库检查失败!");
return;
}
GetPrivateProfileString("OPTION", "Server", "(local)", m_pzServer, sizeof(m_pzServer), databasepath) ;
GetPrivateProfileString("OPTION", "DataBase", "WebGoServer", m_pzDataBase, sizeof(m_pzDataBase), databasepath) ;
GetPrivateProfileString("OPTION", "UserId", "sa", m_pzUser, sizeof(m_pzUser), databasepath) ;
GetPrivateProfileString("OPTION", "PassWord", "sa", m_pzPassWord, sizeof(m_pzPassWord), databasepath) ;
strConnect.Format("driver={SQL Server};SERVER=%s;UID=%s;PWD=%s;DATABASE=%s",
m_pzServer, m_pzUser, m_pzPassWord, m_pzDataBase);
hr = m_pCon.CreateInstance(__uuidof(Connection));//创建Connection对象
if(SUCCEEDED(hr))
{
m_pCon->ConnectionString=(_bstr_t)strConnect;
hr = m_pCon->Open("","","",adModeUnknown);
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!/r/n错误信息:%s",e.ErrorMessage());
::MessageBox(NULL, errormessage, "ERROR", MB_OK|MB_ICONERROR);
CMainFrame *p=(CMainFrame *)AfxGetApp()->m_pMainWnd;
p->DestroyWindow();
}
}
_RecordsetPtr& ADOConn::GetRecordset(_bstr_t bstrSQL)
{
try
{
//连数据库,如果Connection对象为空,则重新连数据库
if(m_pCon==NULL)
OnInitADOConn();
//创建记录集对象
m_pRs.CreateInstance(__uuidof(Recordset));
m_pRs->Open(bstrSQL,m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
//捕捉异常
catch(_com_error *e)
{
AfxMessageBox(e->Description());
}
//返回记录集
return m_pRs;
}
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
try
{
if(m_pCon==NULL)
OnInitADOConn();
m_pCon->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}
_ConnectionPtr ADOConn::GetConnection()
{
return m_pCon;
}
void ADOConn::ExitConnect()
{
if(m_pRs!=NULL)
m_pRs->Close();
m_pCon->Close();
//释放环境
::CoUninitialize();
}
CString ADOConn::AutoNumber(CString sTable, CString sFieldName, CString sCode,int Style)
{
CString sNewNumber;
return sNewNumber;
}
int ADOConn::GetRecordCount(_RecordsetPtr pRst)
{
int count=0;
try
{
pRst->MoveFirst();
}
catch(...)
{
return 0;
}
while(!pRst->adoEOF)
{
pRst->MoveNext();
count=count+1;
}
pRst->MoveFirst();
return count;
}