ADO封装类一共有CConnection,CRecordset,CCommand三个类。
CConnection类
数据成员——私有
_ConnectionPtr m_pConn |
连接智能指针的一个变量 |
CString m_sErrorMessage |
用CConnection类的成员函数产生的错误的文本,可以用GetErrorMessage()成员函数去获取 |
成员函数——私有
SetConnectionString |
设置连接的字符串,这个函数它有两种形式,字符串可以为CString和char *类型的 |
GetConnectionString |
得到连接的字符串 |
SetConnectionTimeout |
设置连接的产生错误前所等待的最长时间 |
GetCommandTimeout |
得到产生错误前所等待的最长时间 |
Open |
打开一个数据库连接,打开的方式都设置默认值,默认Open的四个可选参数都不传递参数。而是用SetConnectionString的方式,这样代码默认情况下很简洁 |
Close |
关闭一个数据库连接 |
Cancel |
取消Open或者Excute操作。调用这个函数必须Open方法的Options参数必须被设置为adSyncConnect,否则会发生run-time错误,即打开方式为同步打开,同步打开的意思是打开成功函数返回,否则一直等待直到超时。我们这个类Open被设置为默认方式打开,也就是同步方式打开。Execute方法的 Options参数必须被设置为adAsyncExecute或者 adAsyncFetch,否则会发生run-time错误,但是我们这个类中不使用connection的Excute函数执行SQL,还是一律选择用CCommand的Excute函数去执行SQL命令。 |
Release |
释放连接智能指针 |
GetCursorLocation |
得到这个CConnection连接的游标类型,这个类在初始化的时候将连接的游标类型设置为了adUseClient |
SetCursorLocation |
设置这个连接的游标类型 |
GetState |
得到这个CConnection的状态,是打开的,关闭的,正在执行SQL命令的,正在连接的状态 |
GetConnection |
得到这个连接的成员变量m_pConn。 |
RollbackTrans |
取消当前事务中所有做的任何更改并结束当前事务。 |
CommitTrans |
取消当前事务中所有做的任何更改并结束当前事务。它也可以开始一个新事务 |
BeginTrans |
开启一个事务 |
GetErrorMessage |
得到调用成员函数时发生的异常信息 |
CRecordset类
数据成员
m_sErrorMessage |
用CRecordset类的成员函数产生的错误的文本,可以用GetErrorMessage()成员函数去获取 |
m_pRst |
记录集智能指针的一个变量 |
成员函数
GetAbsolutePosition |
得到当前记录集游标当前在第几个序号位置 |
SetAbsolutePosition |
设置当前记录集游标到某一个序号位置 |
GetRecordCount |
得到记录集的数目 |
Open |
打开一个记录集,这个函数也给出了两个形式,也就是对连接对象的不同表示,请参见源码 |
Close |
关闭记录集智能指针 |
Release |
释放记录集智能指针 |
MoveNext |
将记录集游标指向下一个记录,这个函数和下面几个记录集游标的移动函数没有加入在出错的情况下给出出错信息 |
MovePrevious |
将记录集游标指向先前的那一条记录 |
MoveLast |
将记录集游标指向最后一条记录 |
MoveFirst |
将记录集游标指向第一条记录 |
Move |
将记录集游标指向指定的那一条记录 |
rstEOF() |
判断当前游标是否指向最后一条记录,如果是还回TRUE,否则还回FALSE |
rstBOF() |
判断当前游标是否指向第一条记录,如果是还回TRUE,否则还回FALSE |
AddNew |
增加记录 |
Delete |
删除当前记录集中某一条记录 |
Cancel |
取消 |
Update |
将当前对记录的改动保存到数据源中 |
SetInt |
改变记录集的某一字段值,并且这个值是int型的。 |
GetInt |
得到某一字段的数据,它还回的数据将会是int类型的。 |
GetString |
得到某一字段的数据,它还回的数据将会是CString类型的。 |
SetString |
改变记录集的某一字段值,并且这个值是CString型的。 |
GetState |
得到记录集的状态 |
CCommand类
数据成员
m_sErrorMessage |
用CCommand类的成员函数产生的错误的文本,可以用GetErrorMessage()成员函数去获取 |
m_pCmd |
命令智能指针的一个变量 |
成员函数
SetActiveConnection |
设置你用的是哪个连接去执行这个命令 |
SetCommandText |
设置你要执行的SQL命令文本 |
SetCommandTimeout(long time) |
设置执行SQL命令的最长时间 |
ExecuteQuery |
执行的命令是查询的SQL语句,它会还回记录集 |
ExecuteUpdate |
执行的命令不会还回记录集,比如像插入,删除类型的SQL语句 |
Release |
释放命令智能指针 |
Cancel |
跟CConnetion的Cancel类似 |
//ADOCONST.h
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // import msado15.dll // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #ifndef YXB_MSADO15DLL_H_H #define YXB_MSADO15DLL_H_H #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","rstEOF") rename("BOF","rstBOF") #endif ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #ifndef YXB_ADOCONST_H_H #define YXB_ADOCONST_H_H namespace ADOCONST { namespace CursorLocation { CursorLocationEnum const adUseNone = CursorLocationEnum(1); //donot use cursor CursorLocationEnum const adUseServer = CursorLocationEnum(2); //default。 CursorLocationEnum const adUseClient = CursorLocationEnum(3); // CursorLocationEnum const adUseClientBatch = CursorLocationEnum(3); // } namespace CursorType { CursorTypeEnum const adOpenUnspecified = CursorTypeEnum(-1); // CursorTypeEnum const adOpenForwardOnly = CursorTypeEnum(0); // CursorTypeEnum const adOpenKeyset = CursorTypeEnum(1); // CursorTypeEnum const adOpenDynamic = CursorTypeEnum(2); // CursorTypeEnum const adOpenStatic = CursorTypeEnum(3); // } namespace LockType { LockTypeEnum const adLockUnspecified = LockTypeEnum(-1); // LockTypeEnum const adLockReadOnly = LockTypeEnum(1); // LockTypeEnum const adLockPessimistic = LockTypeEnum(2); // LockTypeEnum const adLockOptimistic = LockTypeEnum(3); // LockTypeEnum const adLockBatchOptimistic = LockTypeEnum(4); // } namespace ObjectState { ObjectStateEnum const adStateClosed = ObjectStateEnum(0); // ObjectStateEnum const adStateOpen = ObjectStateEnum(1); // ObjectStateEnum const adStateConnecting = ObjectStateEnum(2); // ObjectStateEnum const adStateExecuting = ObjectStateEnum(4); // ObjectStateEnum const adStateFetching = ObjectStateEnum(8); // } namespace ConnectOption { ConnectOptionEnum const adConnectUnspecified = ConnectOptionEnum(-1); // ConnectOptionEnum const adAsyncConnect = ConnectOptionEnum(16); // } namespace CommandType { CommandTypeEnum const adCmdUnspecified = CommandTypeEnum(-1); // CommandTypeEnum const adCmdText = CommandTypeEnum(1); // CommandTypeEnum const adCmdTable = CommandTypeEnum(2); // CommandTypeEnum const adCmdStoredProc = CommandTypeEnum(4); // CommandTypeEnum const adCmdUnknown = CommandTypeEnum(8); // CommandTypeEnum const adCmdFile = CommandTypeEnum(256); // CommandTypeEnum const adCmdTableDirect = CommandTypeEnum(512); // } namespace ExecuteOption { ExecuteOptionEnum const adOptionUnspecified = ExecuteOptionEnum(-1); // ExecuteOptionEnum const adAsyncExecute = ExecuteOptionEnum(16); // ExecuteOptionEnum const adAsyncFetch = ExecuteOptionEnum(32); // ExecuteOptionEnum const adAsyncFetchNonBlocking = ExecuteOptionEnum(64); // ExecuteOptionEnum const adExecuteNoRecords = ExecuteOptionEnum(128); // ExecuteOptionEnum const adExecuteStream = ExecuteOptionEnum(1024); // ExecuteOptionEnum const adExecuteRecord = ExecuteOptionEnum(2048); // } namespace Affect { AffectEnum const adAffectCurrent = AffectEnum(1); AffectEnum const adAffectGroup = AffectEnum(2); AffectEnum const adAffectAll = AffectEnum(3); AffectEnum const adAffectAllChapters = AffectEnum(4); } } #endif
//Connection.h
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Connection.h: + // Author: 楊 小兵 @China + // + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #ifndef YXB_CCONNECTION_H_H #define YXB_CCONNECTION_H_H //////////////////////////////////////////////////////////////////////////////////////////////////// // include flies // //////////////////////////////////////////////////////////////////////////////////////////////////// #include <afx.h> #include "ADOCONST.h" //////////////////////////////////////////////////////////////////////////////////////////////////// using namespace ADOCONST; class CConnection { private: _ConnectionPtr m_pConn; CString m_sErrorMessage; //used to save error message //////////////////////////////////////////////////////////////////////////////////////////////////// //Construction/Destruction // //////////////////////////////////////////////////////////////////////////////////////////////////// public: CConnection(); virtual ~CConnection(); //////////////////////////////////////////////////////////////////////////////////////////////////// // Property Get/Set Method // //////////////////////////////////////////////////////////////////////////////////////////////////// public: //Property: ConnectionString CString GetConnectionString() const; void SetConnectionString(char * charConn); void SetConnectionString(CString strConn); //Property: ConnectionTimeout long GetConnectionTimeout() const; void SetConnectionTimeout(long time); //Property: CursorLocation CursorLocationEnum GetCursorLocation() const; void SetCursorLocation(CursorLocationEnum CursorLocation); //Property: CommandTimeout long GetCommandTimeout() const; void SetCommandTimeout(long time); //Property: State ObjectStateEnum GetState() const; //Property: m_sErrorMessage CString GetErrorMessage() const; //Property: m_pConn _ConnectionPtr GetConnection() const; //////////////////////////////////////////////////////////////////////////////////////////////////// // Core Method // //////////////////////////////////////////////////////////////////////////////////////////////////// public: bool Open(CString ConnectionString,CString UserID="",CString Password="", ConnectOptionEnum ConnectOption=ConnectOption::adConnectUnspecified);//ID和Password为空,因为要再连接字符串中去指明ID和密码 bool Close(); bool Cancel(); void Release(); bool RollbackTrans(); bool CommitTrans(); long BeginTrans(); }; #endif // #ifndef YXB_CONNECTION_H_H //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//Connection.cpp
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Connection.cpp: implementation of the CConnection class. + // Author: 楊 小兵 @China + // + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #include "stdafx.h" #include "Connection.h" using namespace ADOCONST; //////////////////////////////////////////////////////////////////////////////////////////////////// // Construction/Destruction // //////////////////////////////////////////////////////////////////////////////////////////////////// CConnection::CConnection() { CoInitialize(NULL); m_pConn.CreateInstance("ADODB.Connection"); //m_pConn.CreateInstance(__uuidof(Connection)); //default set m_pConn->PutCursorLocation(CursorLocation::adUseClient); } CConnection::~CConnection() { Release(); CoUninitialize(); } //////////////////////////////////////////////////////////////////////////////////////////////////// // Property Get/Set Method // //////////////////////////////////////////////////////////////////////////////////////////////////// //Property: ConnectionString CString CConnection::GetConnectionString() const { return (char *)(m_pConn->GetConnectionString()); } void CConnection::SetConnectionString(CString strConn) { m_pConn->PutConnectionString(_bstr_t(strConn)); } void CConnection::SetConnectionString(char *charConn) { m_pConn->PutConnectionString(_bstr_t(charConn)); } //Property: ConnectionTimeout long CConnection::GetConnectionTimeout() const { return m_pConn->GetConnectionTimeout(); } void CConnection::SetConnectionTimeout(long time) { m_pConn->PutConnectionTimeout(time); } //Property: CursorLocation CursorLocationEnum CConnection::GetCursorLocation() const { return m_pConn->GetCursorLocation(); } void CConnection::SetCursorLocation(CursorLocationEnum CursorLocation) { m_pConn->PutCursorLocation(CursorLocation); } //Property: CommandTimeout long CConnection::GetCommandTimeout() const { return m_pConn->GetCommandTimeout(); } void CConnection::SetCommandTimeout(long time) { m_pConn->PutCommandTimeout(time); } //Property: State ObjectStateEnum CConnection::GetState() const { return (ObjectStateEnum)(m_pConn->GetState()); } //Property: m_sErrorMessage CString CConnection::GetErrorMessage() const { return m_sErrorMessage; } //Property: m_pConn _ConnectionPtr CConnection::GetConnection() const { return m_pConn; } //////////////////////////////////////////////////////////////////////////////////////////////////// // Core Method // //////////////////////////////////////////////////////////////////////////////////////////////////// //Open bool CConnection::Open(CString ConnectionString,CString UserID,CString Password, ConnectOptionEnum ConnectOption) { try { if(m_pConn==NULL) { m_pConn.CreateInstance("ADODB.Connection"); } HRESULT hRsut=m_pConn->Open((_bstr_t)ConnectionString,(_bstr_t)UserID,(_bstr_t)Password,(long)ConnectOption); if(SUCCEEDED(hRsut)) { return true; } else { m_sErrorMessage="There is an error when open the connection!"; return false; } } catch(_com_error e) { //m_sErrorMessage m_sErrorMessage="There is an error when called then function: Close()!"; return false; } }
//Close bool CConnection::Close() { if(m_pConn==NULL) { m_sErrorMessage="Connectin is not available!"; return true; } try { ObjectStateEnum state=(ObjectStateEnum)(m_pConn->GetState()); if(state==ObjectState::adStateOpen) { HRESULT hRsut=m_pConn->Close(); if(SUCCEEDED(hRsut)) { return true; } else { //m_sErrorMessage m_sErrorMessage="Failed to close the connection!"; return false; } } return true; } catch(_com_error e) { //m_sErrorMessage m_sErrorMessage="There is an error when called then function: Close()!"; return false; } } //Cancel bool CConnection::Cancel() { if(m_pConn==NULL) { //m_sErrorMessage m_sErrorMessage="Connectin is not available!"; return false; } try { HRESULT hRsut=m_pConn->Cancel(); if(SUCCEEDED(hRsut)) { return true; } else { //m_sErrorMessage m_sErrorMessage="Failed to cancel!"; return false; } } catch(_com_error e) { //m_sErrorMessage m_sErrorMessage="There is an error when called then function: Cancel()!"; return false; } } //Release void CConnection::Release() { if(m_pConn!=NULL) { try { Close(); m_pConn.Release(); m_pConn=NULL; } catch(_com_error e) { throw e; } } } //BeginTrans long CConnection::BeginTrans() { return m_pConn->BeginTrans(); } //CommitTrans bool CConnection::CommitTrans() { if(m_pConn==NULL) { //m_sErrorMessage m_sErrorMessage="Connectin is not available!"; return false; } try { HRESULT hRsut=m_pConn->CommitTrans(); if(SUCCEEDED(hRsut)) { return true; } else { //m_sErrorMessage m_sErrorMessage="Failed to commit the tranction!"; return false; } } catch(_com_error e) { //m_sErrorMessage m_sErrorMessage="There is an error when called then function: CommitTrans()!"; return false; } } //RollbackTrans bool CConnection::RollbackTrans() { if(m_pConn==NULL) { //m_sErrorMessage m_sErrorMessage="Connectin is not available!"; return false; } try { HRESULT hRsut=m_pConn->RollbackTrans(); if(SUCCEEDED(hRsut)) { return true; } else { //m_sErrorMessage m_sErrorMessage="Failed to rollback the tranction!"; return false; } } catch(_com_error e) { //m_sErrorMessage m_sErrorMessage="There is an error when called then function: RollbackTrans()!"; return false; } } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//Recordset.h
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Recordset.h: implementation of the CRecordset class. + // Author: 楊 小兵 @China + // + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #ifndef YXB_CRECORDSET_H_H #define YXB_CRECORDSET_H_H //////////////////////////////////////////////////////////////////////////////////////////////////// // include flies // //////////////////////////////////////////////////////////////////////////////////////////////////// #include <afx.h> #include "ADOCONST.h" #include "Connection.h" //////////////////////////////////////////////////////////////////////////////////////////////////// // class difine // //////////////////////////////////////////////////////////////////////////////////////////////////// using namespace ADOCONST; class CRecordset { //////////////////////////////////////////////////////////////////////////////////////////////////// // Member // //////////////////////////////////////////////////////////////////////////////////////////////////// private: CString m_sErrorMessage; public: _RecordsetPtr m_pRst; //////////////////////////////////////////////////////////////////////////////////////////////////// // Construction/Destruction // //////////////////////////////////////////////////////////////////////////////////////////////////// public: CRecordset(); virtual ~CRecordset(); //////////////////////////////////////////////////////////////////////////////////////////////////// // Property Get/Set Method // //////////////////////////////////////////////////////////////////////////////////////////////////// public: //Property: RecordCount long GetRecordCount() const; //Property: PageCount long GetPageCount() const; //Property: PageSize long GetPageSize() const; void SetPageSize(long pageSize); //Property: AbsolutePage long GetAbsolutePage() const; void SetAbsolutePage(long page); //Property: AbsolutePosition long GetAbsolutePosition() const; void SetAbsolutePosition(long pos); //Property: State ObjectStateEnum GetState() const; //Property: CursorLocation void SetCursorLocation(CursorLocationEnum CursorLocation); //////////////////////////////////////////////////////////////////////////////////////////////////// // Core Method // //////////////////////////////////////////////////////////////////////////////////////////////////// public: void Open(CString Source, _ConnectionPtr pConn, CursorTypeEnum CursorType=CursorType::adOpenStatic,LockTypeEnum LockType=LockType::adLockOptimistic,long Options=CommandType::adCmdText); void Open(CString Source, CConnection & ActiveConn, CursorTypeEnum CursorType=CursorType::adOpenStatic,LockTypeEnum LockType=LockType::adLockOptimistic,long Options=CommandType::adCmdText); bool Close(); void Release(); //////////////////////////////////////////////////////////////////////////////////////////////////// // Position // //////////////////////////////////////////////////////////////////////////////////////////////////// void MoveNext(); void MovePrevious(); void MoveLast(); void MoveFirst(); void Move(long pos); bool rstEOF(); bool rstBOF(); //////////////////////////////////////////////////////////////////////////////////////////////////// // Recordset update db // //////////////////////////////////////////////////////////////////////////////////////////////////// void AddNew(); void Delete(AffectEnum Option); void Cancel(); void Update(); //////////////////////////////////////////////////////////////////////////////////////////////////// // GetValues form recordset // //////////////////////////////////////////////////////////////////////////////////////////////////// int GetInt(CString columnName); CString GetString(CString columnName); //////////////////////////////////////////////////////////////////////////////////////////////////// // SetValues form recordset // //////////////////////////////////////////////////////////////////////////////////////////////////// void SetInt(CString columnName,int value); void SetString(CString columnName,CString value); }; #endif //#ifndef YXB_CRECORDSET_H_H //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Recordset.cpp
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Recordset.cpp: implementation of the CRecordset class. + // Author: 楊 小兵 @China + // + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #include "stdafx.h" #include "Recordset.h" using namespace ADOCONST; //////////////////////////////////////////////////////////////////////////////////////////////////// // Construction/Destruction // //////////////////////////////////////////////////////////////////////////////////////////////////// CRecordset::CRecordset() { CoInitialize(NULL); m_pRst.CreateInstance("ADODB.Recordset"); //m_pRst.CreateInstance(__uuidof(Recordset)); //default set } CRecordset::~CRecordset() { Release(); } //////////////////////////////////////////////////////////////////////////////////////////////////// // Property Get/Set Method // //////////////////////////////////////////////////////////////////////////////////////////////////// //Property: RecordCount long CRecordset::GetRecordCount() const { return m_pRst->GetRecordCount(); } //Property: PageCount long CRecordset::GetPageCount() const { return m_pRst->GetPageCount(); } //Property: PageSize long CRecordset::GetPageSize() const { return m_pRst->GetPageSize(); } void CRecordset::SetPageSize(long pageSize) { m_pRst->PutPageSize(pageSize); } //Property: AbsolutePage long CRecordset::GetAbsolutePage() const { return m_pRst->GetAbsolutePage(); } void CRecordset::SetAbsolutePage(long page) { m_pRst->PutAbsolutePage((PositionEnum)page); } //Property: AbsolutePosition long CRecordset::GetAbsolutePosition() const { return m_pRst->GetAbsolutePosition(); } void CRecordset::SetAbsolutePosition(long pos) { m_pRst->PutAbsolutePosition(PositionEnum(pos)); } //Property: State ObjectStateEnum CRecordset::GetState() const { return (ObjectStateEnum)m_pRst->GetState(); } //Property: CursorLocation void CRecordset::SetCursorLocation(CursorLocationEnum CursorLocation) { m_pRst->PutCursorLocation(CursorLocation); } //////////////////////////////////////////////////////////////////////////////////////////////////// // Core Method // //////////////////////////////////////////////////////////////////////////////////////////////////// //Open void CRecordset::Open(CString Source,CConnection & ActiveConn,CursorTypeEnum CursorType,LockTypeEnum LockType,long Options) { m_pRst->Open(_variant_t(Source),ActiveConn.GetConnection().GetInterfacePtr(),CursorType,LockType,Options); } void CRecordset::Open(CString Source,_ConnectionPtr pConn,CursorTypeEnum CursorType, LockTypeEnum LockType,long Options) { m_pRst->Open(_variant_t(Source),pConn.GetInterfacePtr(),CursorType,LockType,Options); } //Close bool CRecordset::Close() { if(m_pRst==NULL) { m_sErrorMessage="Recordset is not available!"; return true; } try { ObjectStateEnum state=(ObjectStateEnum)(m_pRst->GetState()); if(state==ObjectState::adStateOpen) { HRESULT hRsut=m_pRst->Close(); if(SUCCEEDED(hRsut)) { return true; } else { m_sErrorMessage="Failed to close the Recordset!"; return false; } } return true; } catch(_com_error e) { m_sErrorMessage="There is an error when called then function: Close()!"; return false; } } //Release void CRecordset::Release() { if(m_pRst!=NULL) { try { Close(); m_pRst.Release(); m_pRst=NULL; } catch(_com_error e) { throw e; } } } //rstBOF bool CRecordset::rstBOF() { return (m_pRst->GetrstBOF()==VARIANT_TRUE?true:false); } //rstEOF bool CRecordset::rstEOF() { return (m_pRst->GetrstEOF()==VARIANT_TRUE?true:false); } //MoveFirst: Move the cursor to the first record. void CRecordset::MoveFirst() { m_pRst->MoveFirst(); } //MoveLast: Move the cursor to the last record. void CRecordset::MoveLast() { m_pRst->MoveLast(); } //MovePrevious void CRecordset::MovePrevious() { if(m_pRst->GetrstBOF()!=VARIANT_TRUE) { m_pRst->MovePrevious(); } } //MoveNext void CRecordset::MoveNext() { if(m_pRst->GetrstEOF()!=VARIANT_TRUE) { m_pRst->MoveNext(); } } //MoveNext void CRecordset::Move(long pos) { if(m_pRst->GetrstEOF()!=VARIANT_TRUE) { m_pRst->Move(pos); } } //////////////////////////////////////////////////////////////////////////////////////////////////// // Recordset update db // //////////////////////////////////////////////////////////////////////////////////////////////////// //AddNew void CRecordset::AddNew() { m_pRst->AddNew(); } //Delete void CRecordset::Delete(AffectEnum Option) { m_pRst->Delete(Option); } //Update void CRecordset::Update() { m_pRst->Update(); } //Cancel void CRecordset::Cancel() { m_pRst->Cancel(); } //////////////////////////////////////////////////////////////////////////////////////////////////// // Method GetValues // //////////////////////////////////////////////////////////////////////////////////////////////////// //GetString CString CRecordset::GetString(CString columnName) { try { return (char *)(_bstr_t)m_pRst->GetCollect(_variant_t(columnName)); } catch(_com_error e) { throw e; } } //GetInt int CRecordset::GetInt(CString columnName) { try { return (short)m_pRst->GetCollect(_variant_t(columnName)); } catch(_com_error e) { throw e; } } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//Command.h
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Command.h: + // Author: 楊 小兵 @China + // + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #ifndef YXB_CCOMMAND_H_H #define YXB_CCOMMAND_H_H //////////////////////////////////////////////////////////////////////////////////////////////////// // include flies // //////////////////////////////////////////////////////////////////////////////////////////////////// #include <afx.h> #include "ADOCONST.h" #include "Connection.h" #include "Recordset.h" //////////////////////////////////////////////////////////////////////////////////////////////////// using namespace ADOCONST; class CCommand { private: _CommandPtr m_pCmd; CString m_sErrorMessage; public: CCommand(); virtual ~CCommand(); //////////////////////////////////////////////////////////////////////////////////////////////////// // Property Get/Set Method // //////////////////////////////////////////////////////////////////////////////////////////////////// public: //Property: ActiveConnection void SetActiveConnection(CConnection &ActiveConn); //Property: CommandText void SetCommandText(CString strCmd); //Property: CommandTimeout void SetCommandTimeout(long time); //Property: CommandType //void SetCommandType(CommandTypeEnum CommandType); //Property: State ObjectStateEnum GetState() const; //////////////////////////////////////////////////////////////////////////////////////////////////// // Other Method // //////////////////////////////////////////////////////////////////////////////////////////////////// public: bool ExecuteQuery(CRecordset &Rst,CommandTypeEnum CommandType=CommandType::adCmdText); bool ExecuteUpdate(long &AffectedRows,CRecordset &Rst,CommandTypeEnum CommandType=CommandType::adCmdText); void Release(); bool Cancel(); }; #endif //#ifndef YXB_COMMAND_H_H //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//Command.cpp
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Command.cpp: implementation of the CConnection class. + // Author: 楊 小兵 @China + // + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #include "stdafx.h" #include "Command.h" using namespace ADOCONST; //////////////////////////////////////////////////////////////////////////////////////////////////// // Construction/Destruction // //////////////////////////////////////////////////////////////////////////////////////////////////// CCommand::CCommand() { CoInitialize(NULL); m_pCmd.CreateInstance("ADODB.Command"); //m_pCmd.CreateInstance(__uuidof(Command)); //default set } CCommand::~CCommand() { Release(); } //////////////////////////////////////////////////////////////////////////////////////////////////// // Property Get/Set Method // //////////////////////////////////////////////////////////////////////////////////////////////////// //Property: ActiveConnection void CCommand::SetActiveConnection(CConnection &ActiveConn) { m_pCmd->PutActiveConnection(ActiveConn.GetConnection().GetInterfacePtr()); } //Property: CommandText void CCommand::SetCommandText(CString strCmd) { m_pCmd->PutCommandText(_bstr_t(strCmd)); } //Property: CommandTimeout void CCommand::SetCommandTimeout(long time) { m_pCmd->PutCommandTimeout(time); } //Property: State ObjectStateEnum CCommand::GetState() const { return (ObjectStateEnum)(m_pCmd->GetState()); } //////////////////////////////////////////////////////////////////////////////////////////////////// // Core Method // //////////////////////////////////////////////////////////////////////////////////////////////////// //ExecuteQuery bool CCommand::ExecuteQuery(CRecordset &Rst,CommandTypeEnum CommandType) { Rst.m_pRst=m_pCmd->Execute(NULL,NULL,CommandType); return true; } //ExecuteUpdate bool CCommand::ExecuteUpdate(long &AffectedRows,CRecordset &Rst,CommandTypeEnum CommandType) { VARIANT rows; rows.vt = VT_I4; Rst.m_pRst=m_pCmd->Execute(&rows,NULL,CommandType); AffectedRows=rows.lVal ; return true; } //Release void CCommand::Release() { if(m_pCmd!=NULL) { try { m_pCmd.Release(); m_pCmd=NULL; } catch(_com_error e) { throw e; } } } //Cancel bool CCommand::Cancel() { if(m_pCmd==NULL) { //m_sErrorMessage m_sErrorMessage="Command is not available!"; return false; } try { HRESULT hRsut=m_pCmd->Cancel(); if(SUCCEEDED(hRsut)) { return true; } else { //m_sErrorMessage m_sErrorMessage="Failed to cancel!"; return false; } } catch(_com_error e) { //m_sErrorMessage m_sErrorMessage="There is an error when called then function: Cancel()!"; return false; } } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//