The ADO access database is based on COM, fast access, convenient use. I found some common methods to create a class in the actual work, asking her for the CadoEx class. The following is the header file, and the file is implemented. If you have anything wrong, please send me a mail luoshizhen@163.net
* Description: ------------------------------------------ * // * Note STDAFX.H must have the following two lines * // * #include
/ * The following is a member of the class * / # ifndef _adoex_h_ # define _adoex_h_
#include "stdafx.h"
Class Cadoex {public: Cadoex (); Virtual ~ Cadoex ();
/ * Open Ado database * / BOOL OpenAdo (LPCTSTR lpDSN, LPCTSTR lpUID, LPCTSTR lpPWD);
File: // ------------------------------------------ // / * ADO general query function * / / * call parameters: the correct SQL query statement. * / / * You can query a table, you can also query multiple tables * / / * Return a record set * / / * If successful Returns a non-empty record * / / * Returns a null record set * / _RecordSetptr query (LPCTSTPTR Query) if failed; file: // ----------------- --------------------------- // File: // ----------------- --------------------------- // * Connect Database * / Bool Closeado (); File: // ------ -------------------------------------- // File: // ------ -------------------------------------- // / * ADO Universal SQL statement execution function * / / * Call parameters: The correct SQL query statement. * / / * Can insert a modified delete action on a table * / / * If successful return true * / / * If the failure returns * / BOOL EXECUTE (LPCTSTR LPEXCUTE); File: // -------------------------------------------- //
Protected: _RecordSetPtr; Char lpbuff [500]; hResult hResult; Bool M_Bopen; _ConnectionPtr M_PConnection;}; # ENDIF
/ * ----------------------------------------------- / * Description: This is an ADO class that can be implemented with ADO to operate * // * 1. Connect Database * // * 2. Query the database or association table * // * 3. Execute SQL statement insertion modification delete operation * // * 4. Turn off the database * /
#include "stdafx.h" #include "adoEx.h"
#ifdef _debug # undef this_filestatic char this_file [] = __ file __; # Define new debug_new # Endif
CADOEX :: Cadoex () {m_bopen = false; / * Initializing connection instance * / m_pconnection.createInstance (_UUIDOF (connection));}
Cadoex :: ~ Cadoex () {
IF (RETRECORDSETPTR! = NULL) RETRECORDSETPTR-> Close (); RetRecordSetPtr = NULL;
CLOSEADO ();
M_Bopen = false;} / * Open ADO Database * / Bool Cadoex :: OpenAdo (LPCTSTSTSTSTSN, LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTS {BOOL BRET = false; if (m_bopen) return true;
Try {hresult = m_pconnection-> open (lpdsn, lpuid, lppwd, 0); if ("" Open Ado Database Succeed! / N "); m_bopen = true; bret = true;} else { m_bopen = false; bret = false;}} / * end of try * / catch (_COM_ERROR E) {MEMSET (LPBUFF, 0X00, 500); Sprintf (LPBUF, "Outthrough / N:% S" when opening the database, E ()); AfxMessageBox (lpbuff);
Return Bret;
/ * ADO general query function * / / * call parameters: the correct SQL query statement. * // * You can query a table, you can also query multiple tables * // * Return a record set * // If successful Returns a non-empty record * // * Returns a NULL record set * / _ recordSetptr Cadoex :: query (lpctstr lpquery) {RETRECORDSETPTR = null; _variant_t vrecsaffected; if (! M_bopen) Return NULL;
try {retRecordsetPtr = m_pConnection-> Execute (lpQuery, & vRecsAffected, adOptionUnspecified);} catch (_com_error e) {memset (lpBuff, 0x00,500); sprintf (lpBuff, "exception occurs when querying the database tables / n:% s", E.ERRORMESSAGE ()); AFXMessageBox (LPBUFF);
Return RetRecordSetptr;}
/ * ADO universal SQL statement execution function * / / * call parameters: the correct SQL query statement. * // * You can insert the modified delete operation to a table * // * If successful return true * // * If the failure returns * / BOOL CADOEX :: EXECUTE (LPCTSTR LPEXCUTE) {BOOL BRET = false; _variant_t vrecsaffected;
IF (! m_bopen) return null;
try {m_pConnection-> Execute (lpExcute, & vRecsAffected, adOptionUnspecified); bRet = TRUE;} catch (_com_error e) {bRet = FALSE; sprintf (lpBuff, "exception occurs when Database Table /; memset (lpBuff, 0x00,500) N:% s ", E.ErrorMessage ()); AFXMessageBox (lpbuff);
Return Bret;
Bool Cadoex :: Closeado () {if (m_bopen) {m_pconnection-> close (); trace0 ("Close Ado Connection! / N");
Return True;}
/ * Description: This is an ADO class that can be implemented with ADO to operate * // * 1. Connect database * /// * 2. Query the database or association table * // * 3. Execute SQL statements Insert modification deletion Operation * // * 4. Turn off the database * /
#include "stdafx.h" #include "adoEx.h" #ifdef _debug # undef this_filestatic char this_file [] = __ file __; # Define new debug_new # Endif
CADOEX :: Cadoex () {m_bopen = false; / * Initializing connection instance * / m_pconnection.createInstance (_UUIDOF (connection));}
Cadoex :: ~ Cadoex () {
IF (RETRECORDSETPTR! = NULL) RETRECORDSETPTR-> Close (); RetRecordSetPtr = NULL;
CLOSEADO ();
M_Bopen = false;} / * Open ADO Database * / Bool Cadoex :: OpenAdo (LPCTSTSTSTSTSN, LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTS {BOOL BRET = false; if (m_bopen) return true;
Try {hresult = m_pconnection-> open (lpdsn, lpuid, lppwd, 0); if ("" Open Ado Database Succeed! / N "); m_bopen = true; bret = true;} else { m_bopen = false; bret = false;}} / * end of try * / catch (_COM_ERROR E) {MEMSET (LPBUFF, 0X00, 500); Sprintf (LPBUF, "Outthrough / N:% S" when opening the database, E ()); AfxMessageBox (lpbuff);
Return Bret;
/ * ADO general query function * / / * call parameters: the correct SQL query statement. * // * You can query a table, you can also query multiple tables * // * Return a record set * // If successful Returns a non-empty record * // * Returns a NULL record set * / _ recordSTPTR CADOEX :: query (lpctstr lpquery) {RetRecordSetPtr = null; _variant_t vrecsaffected;
IF (! m_bopen) return null;
try {retRecordsetPtr = m_pConnection-> Execute (lpQuery, & vRecsAffected, adOptionUnspecified);} catch (_com_error e) {memset (lpBuff, 0x00,500); sprintf (lpBuff, "exception occurs when querying the database tables / n:% s", E.ERRORMESSAGE ()); AFXMessageBox (LPBUFF);
Return RetRecordSetptr;}
/ * ADO universal SQL statement execution function * / / * call parameters: the correct SQL query statement. * // * You can insert the modified delete operation to a table * // * If successful return true * // * If the failure returns * / BOOL CADOEX :: EXECUTE (LPCTSTR LPEXCUTE) {BOOL BRET = false; _variant_t vrecsaffected; if (! M_bopen) Return NULL;
try {m_pConnection-> Execute (lpExcute, & vRecsAffected, adOptionUnspecified); bRet = TRUE;} catch (_com_error e) {bRet = FALSE; sprintf (lpBuff, "exception occurs when Database Table /; memset (lpBuff, 0x00,500) N:% s ", E.ErrorMessage ()); AFXMessageBox (lpbuff);
Return Bret;
Bool Cadoex :: Closeado () {if (m_bopen) {m_pconnection-> close (); trace0 ("Close Ado Connection! / N");
Return True;}