Hello everyone, the time, the root of this rook, published an ADO to access the database articles, got a lot of VC masters, I saw it, the feelings were quite, and the original class continued to be improved, and it is better to use, this class may still exist
Some indifferent places, I hope that the VC masters will be enlightened. The header files are given below, implement the document
-------head File:---------------------------------------- ---------------------------
#ifndef _adoex_h_ # define _adoex_h_
#include "stdafx.h"
/ * Note STDAFX.H must have two lines * // * #include
/ * AFXOLINIT (); ADO initialization * /
#define max_field_num 500 / * The maximum number of fields in a table * /
Class Cadoex {public: Cadoex (); Virtual ~ Cadoex ();
/ * Open ADO Database * / Bool Openado (LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTABLE, LPCTSTSTSTSTSTSTSTSTSTSTABLE, LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTER LPCTSTSTATION, LPPPWD); / * ------------------------- ----------------- * / / * ADO General Query function * / / * call parameters: the correct SQL query statement. * / / * Can query a table or Returns a number of table queries * / / * returns a recordset * / / * Special emphasis can call the stored procedure, the call method is: procedurename param1, param2 * / / *, for example: calling string is "Testado 'Luoshizhen1', ' 1999-05-06 '"" If the successful return of a non-empty record * / / * Returns a null record set * / _RecordSetptr Query (LPCTSTR LPQUERY) if fails * / / * ------------------------------------- * / / * ---------- --------------------------------- * / / * Close database * / bool closeado (); / * - ----------------------------------------- * / / * --- ----------------------------------------- * / / * ADO universal SQL statement execution Function * / / * Call Parameters: The correct SQL query statement. * / / * Can insert a table to modify the delete operation * / / * can call the stored procedure, the call method is: procedurename param1, param2 * / / *, for example: call The string is "Testado 'Luoshizhen1', '1999-05-06'" * / / * If the success return true * / / * If the failed returns * / bool execute (lpctstr lpexcute);
/ * ------------------------------------------ * / public: / * This function is inserted into a record, calling method: * / / * 1 first call OpenADO () success * / / * 2 Setting the member variable m_sfields - the total number of fields of the binding table * / / * 3 Set the member variable array m_sfieldname, m_sfieldnumeric * / / / * m_sfieldprimary * / / * finally populate the new record to the M_SFieldValue array call AddReoCrd () * / bool addRecord (); / * This function is inserted into the binding table Record, calling method: * / / * If the member variable m_sfields, m_sfieldname * / / / * m_sfieldprimary, m_sfieldnumeric * / / * populates the data to be modified to the m_sfieldvalue array, call UpdateReoCrd () * / Bool UpdateRecord (); / * Fill the m_sfieldvalue array, then call this function * / bool deleteRecord (); / * Call the condition behind the parameter where * / / * Returns true * / / * Nothing to meet the requirements Or have an exception to return false * / bool selectmatchrecord (cstring & switch);
/ * Move to the next record of the eligible record * / / * Mobile successfully returns true, returning false * / bool selectnexTrecord () after reaching the record.
int m_nFields; / * total number of fields binding table * / CString m_sFieldName [MAX_FIELD_NUM]; / * array of field names * / CString m_sFieldValue [MAX_FIELD_NUM]; / * array of field values * / BOOL m_bFieldNumeric [MAX_FIELD_NUM]; / * whether the field For numerical type * / bool m_bfieldprimary [max_field_num]; / * Is this field for primary key * /
protected: BOOL GetFieldName (); _RecordsetPtr retRecordsetPtr; _RecordsetPtr m_pFindRecord; _ConnectionPtr m_pConnection; CString ReplaceString (CString & sText); CString m_sTable; / * for binding table name * / char lpBuff [500]; HRESULT hResult; BOOL m_bOpen;};
#ENDIF
------- Implementation documents: --------------------------------------- ---------------------------
/ * 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 (_UUIDEF (Connection)); m_pfindrecord.createInstance (_UUIDOF (Recordset);
m_nfields = 0;}
Cadoex :: ~ Cadoex () {
IF (RETRECORDSETPTR! = NULL) RETRECORDSETPTR-> Close (); RetRecordSetPtr = NULL;
CLOSEADO ();
M_Bopen = false;} / * Open ADO Database * / Bool Cadoex :: Openado (LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTABLE, LPCTSTSTSTSTSTSTS {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; m_stable = lptable } Else {m_bopen = false; bret = false;}} / * end of try * / catch (_COM_ERROR E) {MEMSET (LPBUFF, 0X00, 500); Sprintf (LPBUF, "Output occurs when opening the database / N:% s ", E.ErrorMessage ()); 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 * // * can be called The stored procedure, the call method is: procedurename param1, param2 * / / *, for example: calling string is "Testado 'Luoshizhen1', '1999-05-06'" / * If successful returns a non-empty record set * // * 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: correct SQL query statement. * // * You can insert a modified delete operation * // * You can call the stored procedure, the call method is: ProcedureName Param1, param2 * / / * For example, the string called "Testado 'Luoshizhen1', '1999-05-06'" * // * If successful return true * // * If the failure returns * /
Bool Cadoex :: EXECUTE (LPCTSTR LPEXCUTE) {BOOL BRET = false; _variant_t vrecsaffected;
IF (! m_bopen) Return False;
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;}
/ * Remove a record of the binding table * // * Fill the m_sfieldvalue array, then call this function * / bool cadoex :: deleteRecord () {cstring lpdelete, Temp; _variant_t recordaffect; if (m_stable == ") { AFXMessageBox ("Table name is empty, delete fails!"); Return false;} if (m_stable == 0) {AFXMessageBox ("The total number of fields of the binding table is 0, delete failed!"); Return false;} if ! m_bopen) {AFXMessageBox ("There is no connection to the database, delete failed!"); return false;} lpdelete.format ("delete from% s where", m_stable); intlf = 0; for (INT i = 0; i
IF (m_bfieldnumeric [i]) Temp.format ("% s =% s", m_sfieldname [i], m_sfieldvalue [i]); Else Temp.format ("% s =% s", m_sfieldname [i], replaceString M_SfieldValue [i]));
LPDELETE = TEMP;}}
/ * Remove * / try {m_pconnection-> execute ((lpctstr) LPDelete, & RecordAffect, AdoptionunSpecified;} catch (_COM_ERROR E) {MEMSET (LPBUFF, 0X00, 500); sprintf (lpbuff, "When the binding table occurs Abnormal / N:% S ", E.ErrorMessage ()); AFXMessageBox (LPBUF); Return False;}
Return True;}
CString Cadoex :: ReplaceString (CString & Stext) {CSTRING SRET; CSTRING CCH; for (INT i = 0; i / * Add a record * // * This function inserts a record in the binding table, calling method: * // * 1 first call OpenADO () success * // * 2 Setting the member of this class Variable M_SFields - Total number of fields of the binding table * // * 3 Set the member variable array m_sfieldname, m_sfieldnumeric * / / * m_sfieldprimary * // * Finally populate the new record to the m_sfieldvalue array call AddreoCrd () * / bool Cadoex :: AddRecord () {CSTRING LPINSERT, STEMP; _VARIANT_T RECORDAFFECT; If (! m_bopen) {:: messagebox (null, "yet not connected to the database! / n insert failed!", "Tips", MB_ICONEXCLAMATION | MB_OK); Return False;} if (m_nfields == 0) {:: MessageBox NULL, "The total number of fields of the binding table is not set! / N insert failure!", "Tips", MB_ICONEXCLAMATION | MB_OK); Return False;} lpinsert.format ("INSERT INTO% s (", m_stable; INT ff = 0; for (int i = 0; I / * Insert a record to the binding table * / try {m_pconnection-> execute ((lpctstr) lpinsert, & recordaffect, adoptionunSpecified;} catch (_COM_ERROR E) {MEMSET (LPBUFF, 0X00, 500); Sprintf (lpbuff, " An exception occurred while inserting a record! Insert failed / N:% S ", E.ErrorMessage ()); AFXMessageBox (LPBUF); Return False;} return true; / * Update Binding Table 1 Record * / Bool Cadoex :: UpdateRecord () {CSTRING LPUPDATE, STEMP; _VARIANT_T RECORDAFFECT If (! m_bopen) {:: messagebox (null, "has not connected database, modify fails!", "prompt", MB_ICONEXCLAMATION | MB_OK); Return False;} if (m_nfields == 0) {:: MessageBox (null, "The total number of fields that do not set the binding table, modify the failed!", "Tips", MB_ICONEXCLAMATION | MB_OK); Return False;} LPUPDATE.FORMAT ("Update% s set", m_stable); int ff = 0; for (int i = 0; i AfxMessageBox (lpUpdate); / * update a binding table record * / try {m_pConnection-> Execute ((LPCTSTR) lpUpdate, & RecordAffect, adOptionUnspecified);} catch (_com_error e) {memset (lpBuff, 0x00,500); Sprintf (lpbuff, "an exception occurred in a record of the updated table! Update failed / N:% s", E.ErrorMessage ()); AFXMESSAGEBOX (LPBUFF); Return False;} Return True; / * Find a record of the binding table meet the condition * // * If the record is queried, return true * // * Query does not have an error, but there is no qualified record, return false * // * When the query is abnormal, return Holiday * / Bool Cadoex :: SelectMatchRecord (CString & Swhere) {cstring lpselect, step; _variant_t recordaffect; _variant_t findvalue; If (! m_bopen) {:: messagebox (null, "has not connected database, find failed!", "Tips", MB_ICONEXCLAMATION | MB_OK); RETURN FALSE;} IF (m_nfields == 0) {:: MessageBox (null, "Do not specify the total number of binding table fields, find failed!", "Tips", MB_ICONEXCLAMATION | MB_OK); RETURN FALSE;} IF (Swhere == ") LPselect.format (" SELECT * FROM% S ", M_STABLE); Else Lpselect.Format (" Select * from% s where% s ", m_stable, swhere); AfxMessageBox (lpSelect); / * lookup record * / try {m_pFindRecord = m_pConnection-> Execute ((LPCTSTR) lpSelect, & RecordAffect, adOptionUnspecified); (! M_pFindRecord-> GetadoEOF ()) if {for (int i = 0; i hMoveNext = m_pFindRecord-> MoveNext (); if (SUCCEEDED (hMoveNext)) {if {for (m_pFindRecord-> GetadoEOF ()!) (int i = 0; i } / * All fields of the table * / bool cadoex :: getfieldname () {_variant_t recordaffect; lpstr lpselect; _recordsetptr m_recordset; fieldptr m_fieldptr; IF (! m_bopen) returnaf false; lpselect = new char [100]; Memset ((char *) lpselect, 0x00, 100); sprintf (lpselect, "select * from% s", m_stable); m_recordset = m_pconnection-> execute (LPSELECT, & Recordaffect, AdoptionunSpecified); m_fieldptr = m_recordset-> getfields (); Delete lpselect; Return True;}