An ole

zhaozj2021-02-16  52

OLE_DB is based on COM interface, its access speed, efficiency is the highest, ADO is based on ole_db,

Efficiency is under OLE_DB, ODBC is more useful, (don't shoot bricks!). But ole_db is related

There are very little information, I am most practical to study OLE_DB, now packaged into a class, now open, there is deficiency

Where do you advice.

This is the header of the class: ------------------

#if! defined (AFX_ADADATABASE_H__CC00630B_09FF_483D_81AB_7DC19E3EA26A__INCLUDED _) # Define AFX_ADODATABASE_H__CC00630B_09FF_483D_81AB_7DC19E3EA26A__INCLUDED_

#if _MSC_VER> 1000 # prgma overce # ENDIF

#define max_fields 255 / * A number of words in a table * / # define max_fields_len 30 / * Field name Maximum number * / # define max_fields_data_len 280 / * Character in the character field * /

class COLEDBDatabase {public: void MyGetColumnValue (); void ConvertNumericToChar (); void RollbackTran (); void CommitTran (); void StartTran (); BOOL NextMatchingRecord (); int SelectMatchingRecords (char * szWhere); BOOL ExecuteQuery (char * szQuery) ; BOOL UpdateRecord (char * szWhere); void Quote (char * sText); BOOL AddRecord (); BOOL ExecuteSQL (char * szSQL); BOOL ConnectDB2 (char * szServerName, char * szDatabase, char * szUid, char * szPwd); Bool Connectdb1 (Char * Szdns, Char * Szuid, Char * Szpwd); Bool ConnectDB (CDBPropset Dbpropset); Void DisconnectionDB (); Cadodatabase (); Virtual ~ Cadodatabase ();

/ * ----------- Replacement --------------------------- * / String Szold, Sznew; int NPOS, NGET; BOOL BREPLACE

/ * ------------ OLE DB operation ------------------------- * / char m_provider [ 20] / * Database provides msdasql sqloledb * / cDataSource m_datasource; / * Data source handle * / csession m_session; / * session handle * / ccommand m_cmdexecute; / * public execution SQL handle [note, no line Set return] * / hinstance hinstance; / * get the wrong handle * / ccommand m_cmdquery; / * ---------------- Column information ---- ------------ * / dbstatus dbstatus; / * Used to determine if the column is empty * / dbcolumninfo * m_pcolsinfo; / * column information * / ulong m_ncolcount; / * Column total * / lpolestr m_pstrbuffer ; / ** / unsigned long ncoltype; / * Column type * / long dblong; / * Used to take the shaped column * / db_numeric dbnumeric; / * Used to take numeric column * / char sznumeric [20]; / * for assistance Take numeric * / unsigned int nNumericlen; / * Used to take numeric column * / __int64 dbint64; / * Used to take Nume RIC column only * /

/ * -------------------- Time, Date Processing ------------------- * / dbtime DBTime; / * Time 20:12:20 * / DBDATE DBDATE; / * Date 2003-10-31 * / dbtimestamp dbtimestamp; / * Timestamp 2003-10-31 20:12:20 * /

BOOL M_BOPEN; / * Do not open * / char m_szecute [1024 * 3]; / * Execute statement * / char m_szerrmsg [1024]; / ​​* Error message * / hResult HR; int FF; String sztext1;

/ * ------------- Temporary binding table ------------------- * / char m_sztablename [255]; / * temporary Binding table name * / int m_nfields; / * Timed binding table field * / char m_sfieldname [max_fields] [MAX_FIELDS_LEN]; / * Temporary binding table field name * / char m_sfieldValue [max_fields] [max_fields_data_len]; / * Temporary binding table field value * / int m_bnumeric [max_fields]; / * Temporary binding table field is numeric type 1 int 2 varchar 3 Numeric 4 date * / protected: void getExeCEcROR ();

#ENDIF

The following is an implementation file of class --------------------------

/ * Luo Family Brothers 2004-05-12 * /

#include "stdafx.h" #include "oledbdatabase.h"

ColedbDatabase :: ColedBDatabase () {m_bopen = false; szold = "'"; sznew = "' '"; hINSTANCE = NULL;}

ColedbDatabase :: ~ ColedBDatabase () {

}

/ * Disconnected * / void ColedBDatabase :: DisconnectionDB () {if (m_bopen) {m_cmdexecute.releaseCommand (); file: // must call * / m_cmdquery.releaseCommand (); file: // must call * / m_session. Close (); m_datasource.close (); m_bopen = false;} if (hinstance! = Null) {/ * release handle * / :: freeelibrary (hinstance); hinstance = null;}}

/ * Connect to the database * / BOOL COLEDBDatabase :: ConnectDB (CDBPropSet dbPropSet) {if (m_bOpen) DisconnectionDB (); hInstance = :: LoadLibrary ( "MSDAERR.DLL"); hr = m_DataSource.Open (m_Provider, & dbPropSet); if ( Failed (hr)) goto end; hr = m_session.open (m_datasource); if (failed (hr)) goto end; m_bopen = true; return true; end: {m_bopen = false; getExecerror (); return false;}} / * Connect Database 1 SZDNS -> ODBC DNS SZUID -> User Name SZPWD -> Password * / Bool ColedBDatabase :: Connectdb1 (Char * SZDNS, Char * Szuid, Char * SZPWD) {CDBPropset Dbinit (dbpropset_dbinit);

dbinit.AddProperty (DBPROP_INIT_DATASOURCE, szDNS); dbinit.AddProperty (DBPROP_AUTH_USERID, szUid); dbinit.AddProperty (DBPROP_AUTH_PASSWORD, szPwd); dbinit.AddProperty (DBPROP_INIT_PROMPT, (short) 4); dbinit.AddProperty (DBPROP_INIT_LCID, (long) 1033) ; STRCPY (M_Provider, "MSDasql"); return connectdb (dbinit);}

/ * Connection Database Description: SZSERVERNAME -> is the machine name or IP address Szuid -> User name szpwd -> password * / boledbdatabase :: connectdb2 (char * szservername, char * szdatabase, char * szuid, char * szpwd) {cdbpropset Dbinit (dbpropset_dbinit);

strcpy (m_Provider, "SQLOLEDB"); dbinit.AddProperty (DBPROP_INIT_DATASOURCE, szServerName); dbinit.AddProperty (DBPROP_INIT_CATALOG, szDatabase); dbinit.AddProperty (DBPROP_AUTH_USERID, szUid); dbinit.AddProperty (DBPROP_AUTH_PASSWORD, szPwd); dbinit.AddProperty (DBPROP_INIT_PROMPT (Short) 4); dbinit.addproperty (dbprop_init_lcid, (long) 1033);

Return connectdb (dbinit);

/ * Perform a SQL statement * / boleDbdatabase :: executesql (char * szsql) {if (! M_bopen) Return False;

hr = m_CmdExecute.Open (m_Session, szSQL); if (FAILED (hr)) {GetExecError (); m_CmdExecute.Close (); m_CmdExecute.ReleaseCommand (); return FALSE;} m_CmdExecute.Close (); m_CmdExecute.ReleaseCommand () Return True;}

/ * Add a record * / BOOL COLEDBDATABASE :: addRecord () {if (! M_bopen) returnaf false;

IF (m_nfields <= 0) Return False;

FF = 0; Sprintf (m_szexecute, "INSERT INTO% S (", M_SZTABLENAME); for (int i = 0; i 0) STRCAT (m_szexecute, ","); strcat M_SZEXECUTE, M_SFIELDNAME [I]); FF ;} strcat (m_szexecute, ") Values ​​("); ff = 0; for (i = 0; i 0) strcat (m_szexecute , ","); If (m_bnumeric [i] == 1 || m_bnumeric [i] == 3) strcat (m_szeecute, m_sfieldvalue [i]); else {strcat (m_szexecute, "'"); quote (m_sfieldvalue [ I]); strcat (m_szexecute, m_sfieldvalue [i]); strcat (m_szexecute, "'");}

FF ;}

STRCAT (M_SZEXECUTE, ")"); return executesql (m_szexecute);}

/ * Replacement 'to' 'to prevent error recommendations * / void ColedbDatabase :: quote (char * stext) {sztext1 = step; breplace = false; npos = 0; nget = 0; while (Nget! = - 1 ) {Nget = sztext1.Find (szold, npos); if (nget> 0) {sztext1.replace (nget, szold.length (), sznew); npos = nget 2; breplace = true;}} f (BREPLACE ) STRCPY (Stext, sztext1.c_str ());}

/ * Modify a record to set the data, bring the modification condition, execute * / booredbdatabase :: updateRecord (char * szwhere) {if (! M_bopen) returnaf false;

IF (m_nfields <= 0) Return False;

Sprintf (M_SZEXECUTE, "Update% s set", m_sztablename); ff = 0; for (int i = 0; i 0) STRCAT (m_szexecute, ","); strcat (m_szexecute , m_sfieldname [I]); strcat (m_szexecute, "="); if (m_bnumeric [i] == 1 || m_bnumeric [i] == 3) STRCAT (m_szexecute, m_sfieldvalue [i]); else {strcat (m_szexecute , "'"; Quote (m_sfieldvalue [i]); / * Protect * / strcat (m_szeecute, m_sfieldvalue [i]); strcat (m_szexecute, "'");} ff ;} strcat (m_szexecute, " "); strcat (m_szexecute, szwhere); return executesql (m_szexecute);}

/ * Execute Query * / Bool ColedBDatabase :: EXECUTEQUERY (CHAR * SZQUERY) {if (! M_bopen) Return False; hr = m_cmdquery.open (m_session, szquery); if (Failed (HR)) {/ * error handling * / GetExecerror (); m_cmdquery.close (); return false;} return true;

/ * Find record parameters: WHERE can return the value: 1-> Has a qualified value 0-> No qualified value -1-> error * / int cableDbDatabase :: selectmatchingRecords (char * szwhere) {if (! m_bopen) Return False;

IF (m_nfields <= 0) Return False;

Sprintf (M_SZEXECUTE, "SELECT"); ff = 0; for (int i = 0; I 0) strcat (m_szecute, ","); strcat (m_szexecute, m_sfieldname [i] ); ff ;} strcat (m_szExecute, "From"); strcat (m_szExecute, m_szTableName); strcat (m_szExecute, "Where"); strcat (m_szExecute, szWhere);! if (ExecuteQuery (m_szExecute)) return -1 ;

HR = m_cmdquery.movefirst (); if (hr! = s_ok) {/ * no record * / m_cmdQuery.close (); return 0;} else {/ * has record * / mygetColumnValue ();} return1;}

/ * Next Record * / BOOL COLEDBDATABASE :: nextmatchingRecord () {hr = m_cmdquery.movenext (); if (hr! = S_ok) {/ * Mobile failed * / m_cmdQuery.close (); return false;} else {/ * Mobile success * / mygetColumnValue ();} return true;} void coledbdatabase :: starttran () {if (! M_bopen) return; m_session.startTransAction ();

Void ColedBDatabase :: CommitTran () {if (! m_bopen) return; m_session.commit ();

Void ColedBDatabase :: rollbacktran () {if (! m_bopen) return; m_session.abort ();

/ * Get an error * / void coledbdatabase :: getExecerror () {lpvoid lpmsgbuf;

if (hInstance) {long l = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM, hInstance, hr, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) & lpMsgBuf, 0, NULL);

LSTRCPY (m_szerrmsg, (lptstr) lpmsgbuf);

LocalFree (lpmsgbuf);

}

/ * Convert the format for the label DB_NUMERIC XXXXX.XXX * / void COLEDBDatabase :: ConvertNumericToChar () {memcpy ((char *) & dbInt64, dbNumeric.val, sizeof (__ int64)); memset (szNumeric, 0x00, sizeof (szNumeric)); _i64toa (dbInt64, szNumeric, 10); nNumericLen = strlen (szNumeric); if (dbNumeric.scale> 0) {memmove (szNumeric (nNumericLen-dbNumeric.scale 1), szNumeric (nNumericLen-dbNumeric.scale), dbNumeric.scale ); SZNUMERIC [NNUMERICLEN-DBNUMERIC.SCALE] = '.';} If (dbnumeric.sign == 0) {MEMMOVE (SZNUMERIC 1, SZNUMERIC, STRLEN (SZNUMERIC)); SZNUMERIC [0] = '-';} }

/ * Get the value for each column * / void COLEDBDatabase :: MyGetColumnValue () {m_CmdQuery.GetColumnInfo (& m_nColCount, & m_pColsInfo, & m_pStrBuffer); for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-22519.html

New Post(0)