In Visual C or C Builder, we can package the ADO's operation into a class by accessing the COM object of ADO. First, we need to import MSADO15.DLL to generate type libraries. In C Builder, use Tlibimp.exe to generate files and other files, and then we can write this class by reading the information in the type library: .h file # ifndef adoh # define adoh # include "adoDB_TLB .h "#include" utilcls.h "#include
class ADOConnection {public: ADOConnection (); virtual ~ ADOConnection (); public: _ConnectionPtr m_pConnection; BOOL m_bConnectionOpen; BOOL m_bInstance; int m_nOpenMode; BOOL Open (BSTR dsn, BSTR uid, BSTR pwd);}; class ADOrecordset {public: ADOrecordset (); virtual ~ ADOrecordset (); public: _ConnectionPtr m_pConnection; _RecordsetPtr m_recordset; BOOL m_bOpen; BOOL SetConnect (_ConnectionPtr _pConnection); BOOL Open (WideString sql); HRESULT GetFieldValue (Variant idx, Variant * newVal); cValue GetFieldValue (String sName ); cValue GetFieldValue (long index); HRESULT GetFieldCount (long & Count); // added by chenbin HRESULT GetFields (long index, String & sFieldList); // added by chenbin HRESULT GetFieldType (FieldPtr pField, String & strMsg, long & nResult); / / added by chenbin HRESULT GetFieldType (String FieldName, String & strMsg, long & nResult); // added by chenbin BOOL get_BOF (); BOOL get_EOF (); HRESULT Prev (); HRESULT Last (); HRESULT Next (); HRESULT First ( ); HResult closerecordset (void);}; # ENDIF
.cpp file # iDef __borlandc __ # include
File: // if Necessary, You May Add Codes Here ......} CVALUE :: ~ CVALUE () {file: // if Necessary, You May Add Codes Here .......} BOOL CVALUE:: asbool () {if (m_value.isempty ()) Return False; else return m_value.astype (varboolean);} string cvalue :: asstring () {i (m_value.isempty ()) Return ""; else return M_Value.astype (Varstring);} int CValue :: asint () {int m_int; if (m_value.isempty ()) Return 0; else {switch (m_value.type ()) {copy varsmallint: m_int = m_Value.astype Varsmallint); Break; Case Varinteger: m_int = m_value.astype (varinteger); Break; default: m_int = 0;}} Return M_INT;} float cvalue :: asfloat () {float m_float; if (m_value.isempty ()) Return 0; Else {Switch (M_Value.Type ()) {CASE VARSINGLE: M_FLOAT = m_value.astype (Varsingle); Break; Case Vardouble: m_float = m_value.astype (VARDOUBLE); BREAK; Default: M_float = 0; break;}} return m_float;} tdatetime cvalue :: asdate () {file: // 1970-01-01 12:00:00 is invalid datetime tdatetime dt = strattime ("1970-01-01 12: 00:00 "); if (m_value.isempty ()) Return DT; Else Return M_Value.astype (Vardate);
File: // ---------------- Class Adoconnection ------------ Adoconnection :: adoconnection () {m_bconnectionOpen = false; m_binstance = false; file : // if Necessary, You May Add Codes Here ....} Adoconnection :: ~ Adoconnection () {m_binstance = false; m_bconnectionopen = false; file: // if Necessary, You May Add Codes here ....} Bool ADOConnection :: Open (wchar_t * dsn, wchar_t * uid, wchar_t * pwd) {HRESULT hr; if (! m_bInstance) {try {hr = m_pConnection.CreateInstance (__ uuidof (Connection));} catch (Exception & e) {:: MessageBox (:: GetActiveWindow (), e.Message.c_str (), "Error", MB_OK MB_ICONERROR); return FALSE;} m_bInstance = true;} if (m_bConnectionOpen) return TRUE; try {hr = m_pConnection-> Open ( DSN, UID, PWD, AdmodeunkNown; m_nopenmode = 2;} catch (exception & e) {:: messagebox (: getActiveWindow (), E.MESSAGE.C_STR (), "error", MB_OK MB_ICONERROR); RETURN FALSE; } M_bconnectionOpen = true; hr = s_ok; return true;} file: // --- ---- Class Adorecordset ------------ AdorecordSet :: adorecordset () {} adorecordset :: ~ adorecordset () {} Bool AdorecordSet :: setConnect (_ECTIONPTR _PCONNECTION) {i (_pconnection) {i (_pconnection) { HRESULT hr = m_recordset.CreateInstance (__ uuidof (Recordset)); m_pConnection = _pConnection; if (SUCCEEDED (hr) == S_OK) return TRUE; else return FALSE;} else return FALSE;} BOOL ADOrecordset :: Open (WideString sql) { try {_CommandPtr m_Cmd ( "ADODB.Command"); m_Cmd -> _ set_ActiveConnection (m_pConnection); m_Cmd-> set_CommandText (sql); m_Cmd-> set_CommandType (adCmdText); m_recordset = m_Cmd-> Execute (NULL, NULL, adCmdText);
m_bOpen = true;} catch (Exception & e) {m_bOpen = FALSE; return FALSE;} return TRUE;} HRESULT ADOrecordset :: GetFieldValue (Variant idx, Variant * newVal) {FieldsPtr * Fields = 0; FieldPtr * Field = 0; HRESULT HR = m_recordset-> get_fields (Fields); if (hr == s_ok) {hr = (* fields) -> get_item (idx, field); if (hr == s_ok) {hr = (* field) -> get_Value (* newval);}}
} CValue ADOrecordset :: GetFieldValue (String sName) // Get Field's value according to the field's name {FieldsPtr * Fields = 0; FieldPtr * Field = 0; CValue v; Variant idx = sName; if (m_recordset-> get_Fields (Fields) == S_OK) {if (* fields) -> get_item (idx, field) == s_ok) {v.m_value = (* field) -> get_value ();}} returnv;} CVALUE ADORECORDSET :: GetFieldValue Long index) {Fieldsptr * fields = 0; Fieldptr * field = 0; CVALUE V; if (m_recordset-> get_fields (fields) == s_ok) {if ((* fields) -> get_item (index, field) == s_ok ) {V.m_value = (* field) -> get_value ();}} returnv;} HRESULT ADORECORDSET :: GetFieldCount (long & count) {Fieldsptr * Fields; HRESULT HR; hr = m_recordset-> get_fields (fields); if (hr == S_OK) {hr = (* Fields) -> get_Count (& Count);} return hr;} HRESULT ADOrecordset :: GetFields (long index, String & sFieldList) {FieldsPtr * Fields; FieldPtr * Field; HRESULT hr; WideString FieldName; long REC = 0; hr = m_recordset-> get_fields (field s); if (hr == s_ok) {hr = (* fields) -> get_count (& REC); if (hr == s_ok) {for (int i = 0; i