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; } } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//