Connect to a database using ADO

Source: Internet
Author: User

# 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.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.