MFC Learning (i)--ado database programming

Source: Internet
Author: User

These are in the MFC framework for the study of insurance projects, only studied for two weeks, but still learned a lot of knowledge. Here is a look at the various operations of the database using ADO. 1, the database connection and open the project is SQL Server database, ADO connection data encountered the most important problem is to find the connection string, through a variety of personal experience, found on the internet a bug-free method, is by creating a new Ado.udl file and then opening the appropriate database and opening it with Notepad to get the connection string. Here's a way to open a database in my package:

1 BOOL cdateoperator::opendatebase ()2 {3 4     if(NULL = =m_pconnection) {5AfxMessageBox (L"Create Instance failed!");6         returnFALSE;7     }8     if(m_pconnection) {9         Ten          One         Try A         { -              -             Const_bstr_t strsrc="provider=sqloledb.1;integrated Security=sspi; Persist Security info=false;initial catalog=mydb;data source=li5ch\\sqlexpress"; the  -HRESULT hr = M_pconnection->open (strsrc,l"", L"",-1); -         } -         Catch(_com_error &e) +         { - CString errormsg; +ErrorMsg. Format (L"Close Connection--operation Error! \ r \ n Error message:%s", E.errormessage ()); A AfxMessageBox (errormsg); at              -         } -  -     } -     returnTRUE;  -  in}

2. How ADO executes SQL statements

BOOL cdateoperator::excutesql (CString strsql) {    if(NULL = =    return  FALSE    ; Try         {                    = M_pconnection->execute (_bstr_t (strSQL), null,-1);        }         Catch (_com_error &E)        {            return  FALSE;        }         return TRUE;}

3. How ADO Inserts Records

BOOL cdateoperator::insert_db (DEMO objuser) {if(NULL = = m_pconnection)returnFALSE;    _RecordsetPtr M_precordset; HRESULT HR= M_precordset.createinstance (L"ADODB. Recordset"); if(FAILED (HR))returnFALSE; CString strSQL= L"SELECT user_id, user_name from Fate"; HR= m_precordset->Open (_bstr_t (strSQL), M_pconnection.getinterfaceptr (), adopenstatic,adlockoptimistic,adcmdtext); if(FAILED (HR)) {m_precordset.release (); returnFALSE; }    Try{M_precordset-AddNew (); }    Catch(_com_error &e) {M_precordset-Close ();        M_precordset.release (); returnFALSE; }//byte bydata[10000];    Try{M_precordset->putcollect ("user_id", _variant_t (objuser.        USER_ID)); M_precordset->putcollect ("user_name", _variant_t (objuser.    USER_NAME)); }    Catch(_com_error &e) {M_precordset-Close ();        M_precordset.release (); returnFALSE; } M_precordset-Update (); M_precordset-Close ();    M_precordset.release (); returnTRUE;}

4. How ADO Queries Data

BOOL cdateoperator::select_db (vector<demo>&Vecobjuser) {    if(NULL = = m_pconnection)returnFALSE;    _RecordsetPtr M_precordset; HRESULT HR= M_precordset.createinstance (L"ADODB. Recordset"); if(FAILED (HR))returnFALSE;    Vecobjuser.clear (); CString strSQL= L"SELECT user_id, user_name from Fate"; HR= m_precordset->Open (_bstr_t (strSQL), M_pconnection.getinterfaceptr (), adopenstatic,adlockoptimistic,adcmdtext); if(FAILED (HR)) {m_precordset.release (); returnFALSE; } variant_bool BRet= m_precordset->getadoeof ();  while(!BRet) {_variant_t Varuserid= M_precordset->getcollect (L"user_id"); _variant_t Varusername= M_precordset->getcollect (L"user_name");        DEMO objuser; objuser.user_id=Varuserid.intval; _tcscpy (Objuser.user_name, (TCHAR*) (_bstr_t) varusername);        Vecobjuser.push_back (objuser); M_precordset-MoveNext (); BRet= m_precordset->getadoeof (); } M_precordset-Close ();    M_precordset.release (); returnTRUE; }

5. How ADO Updates data

BOOL cdateoperator::update_db (DEMO objuser) {if(NULL = = m_pconnection)returnFALSE;    _RecordsetPtr M_precordset; HRESULT HR= M_precordset.createinstance (L"ADODB. Recordset"); if(FAILED (HR))returnFALSE;    CString strSQL; Strsql.format (L"SELECT user_id, user_name from Fate WHERE user_id=%d", objuser.    USER_ID); HR= m_precordset->Open (_bstr_t (strSQL), M_pconnection.getinterfaceptr (), adopenstatic,adlockoptimistic,adcmdtext); if(FAILED (HR)) {m_precordset.release (); returnFALSE; }//byte bydata[10000];    Try{M_precordset->putcollect ("user_name", _variant_t (objuser.    USER_NAME)); }    Catch(_com_error &e) {M_precordset-Close ();        M_precordset.release (); returnFALSE; } M_precordset-Update (); M_precordset-Close ();    M_precordset.release (); returnTRUE;}

6, paste the following structure and destructor

Cdateoperator::cdateoperator (void){    Try{HRESULT hr= M_pconnection.createinstance ("ADODB. Connection"); if(FAILED (HR)) {AfxMessageBox (L"Create Instance failed!"); M_pconnection=NULL; }    }    Catch(_com_error &e) {}}cdateoperator::~cdateoperator (void){    if(m_pconnection) {Try{HRESULT hr= m_pconnection->Close (); }        Catch(_com_error &e) {}} m_pconnection.release (); M_pconnection=NULL; }

MFC Learning (i)--ado database programming

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.