# Import "C: \ Program Files \ common files \ System \ ADO \ msado15.dll" no_namespace \ Rename ("EOF", "adoeof") Rename ("Bof", "adobof ")
// Add a dynamic link library and rename the ending indicator to avoid duplication with the System
class ADOConn {public:BOOL ExecuteSQL(_bstr_t bstrSQL);ADOConn();virtual ~ADOConn();_ConnectionPtr m_pCon;_RecordsetPtr m_pRecordset;void OnInitADOConn();void ExitConnect();_RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);};
Void adoconn: oninitadoconn () {: coinitialize (null); try {m_pcon.createinstance ("ADODB. connection "); _ bstr_t strconnect =" xxxxxxxxxxxx "; // connection string m_pcon-> open (strconnect," "," ", admodeunknown);} catch (_ com_error E) {afxmessagebox (E. description () ;}} void adoconn: exitconnect () {If (m_precordset! = NULL) {m_precordset-> close () ;}m_pcon-> close ();: couninitialize () ;}// execute the SQL statement to return the pointer to the recordset object
_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL){try{if(m_pCon==NULL)OnInitADOConn();m_pRecordset.CreateInstance(__uuidof(Recordset));m_pRecordset->Open(bstrSQL,m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);}catch(_com_error e){e.Description();}return m_pRecordset;}BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL){_variant_t RecordsAffected;try{if(m_pCon==NULL)OnInitADOConn();m_pCon->Execute(bstrSQL,NULL,adCmdText);return true;}catch(_com_error e){e.Description();return false;}}
// Obtain a connection string: 1. first, add the component to the project in the project directory and select component and controls gallery. 2. in the displayed dialog box, select registered ActiveX controls 3. in the Open folder, select Microsoft ADO Data Control 6.0 and insert 4. add the control to the window as needed. Right-click the control and select general label 5 from the shortcut menu. select use connection string, click build to select the database type you want to connect to, and follow the prompts to perform the operation. After the test connection is successful, the required connection string will be displayed in use connection string.