C + + operates on database through ADO

Source: Internet
Author: User


C + + through ADO on database operations

Example: C + + writes data to SQL Server via ADO call stored procedure


1. Import the Microsoft-provided ADO dynamic library into the. h header File

#pragma warning (disable:4146)

#import "C:\\Program Files\\Common Files\\system\\ado\\msado15.dll" Named_guids rename ("EOF", "adoeof"), rename ("BOF" , "Adobof")

#pragma warning (default:4146)

using namespace ADODB;


2. Writing a function call stored procedure

Save Database records

void Savedbtran (char *devid,char * Traceno,char *refno,char *batchno, char * bankcardno, char * trandate, char * trantime, char * Amt,char * Hoscardno,char * Hosname,char * hosidcardno, char *msg)

{


2.1 Creating a database connection handle

_connectionptr Pmyconnect;

HRESULT hr;

Try

{

2.1.1 To build a connection database string

Char *dbconstr = new char[400];

strcpy (Dbconstr, "provider=sqloledb; Server=sqlserver server address, 1433;database= database name, uid= login user; pwd= login password ");

CoInitialize (NULL);

Sleep (300);

2.1.2 handle instantiation to implement a connection

hr = Pmyconnect.createinstance ("ADODB. Connection ");

if (SUCCEEDED (HR))

{

Ct. Savelog ("Create database Connection object successfully, wait for open connection", 0,dbconstr);

Pmyconnect->open (_bstr_t (DBCONSTR), "", "", adModeUnknown);

}

Else

{

CoUninitialize ();

Return

}

}

catch (_com_error &err)

{

CoUninitialize ();

return;

}

2.2 Build Run command parameter handle

_commandptr M_pcommand;

_RecordsetPtr M_precordset;

Try

{

2.2.1 Handle instantiation

M_pcommand.createinstance (__uuidof Command);//command no need to change

M_pcommand->activeconnection = Pmyconnect;//pmyconnect is the connection database handle

M_pcommand->commandtype = Adcmdstoredproc;//adcmdstoredproc without change

M_pcommand->commandtext = _bstr_t ("P_d_tra");//p_d_tra is the name of the stored procedure


2.2.2 Set to variable parameter assignment

_variant_t Vv_dotype,vv_tranno,vv_devid,vv_trandate,vv_trantime;

_variant_t Vv_retcode,vv_bustype,vv_cardno,vv_idcard,vv_name;

_variant_t Vv_amt,vv_notes,vv_serid,vv_descont,vv_tranflag;

_variant_t Vv_bankcardno,vv_bankgroupno,vv_bankflowno,vv_devname,vv_execount;

Value to Variant

Vv_dotype =_variant_t (_bstr_t ("1"));

Vv_tranno =_variant_t (_bstr_t (Temp_traceno));

Vv_devid =_variant_t (_bstr_t (DevID));

Vv_trandate =_variant_t (_bstr_t (datetime));

Vv_trantime =_variant_t (_bstr_t (datetime));

Vv_retcode =_variant_t (_bstr_t (""));

Vv_bustype =_variant_t (_bstr_t ("14"));

Vv_cardno =_variant_t (_bstr_t (Hoscardno));

Vv_idcard =_variant_t (_bstr_t (Hosidcardno));


Vv_name = _variant_t (_bstr_t (hosname));

Vv_amt =_variant_t (_bstr_t (AMT));

Vv_notes =_variant_t (_bstr_t ("Trading Success"));

Vv_serid =_variant_t (_bstr_t (""));

Vv_descont =_variant_t (_bstr_t (""));

Vv_tranflag =_variant_t (_bstr_t ("0"));

Vv_bankcardno =_variant_t (_bstr_t (Temp_bankcardno));

vv_bankgroupno=_variant_t (_bstr_t (Batchno));

Vv_bankflowno =_variant_t (_bstr_t (REFNO));

Vv_devname =_variant_t (_bstr_t (""));

Vv_execount = _variant_t (Ret_execount);

2.2.3 Creating database stored procedure input parameters

_parameterptr Mp_dotype,mp_tranno,mp_devid,mp_trandate,mp_trantime;

_parameterptr Mp_retcode,mp_bustype,mp_cardno,mp_idcard,mp_name;

_parameterptr Mp_amt,mp_notes,mp_serid,mp_descont,mp_tranflag;

_parameterptr Mp_bankcardno,mp_bankgroupno,mp_bankflowno,mp_devname,mp_execount;


2.2.3.1 to instantiate database parameters

Create Parameterptr instance

Mp_dotype. CreateInstance (__uuidof (Parameter));

Mp_tranno.createinstance (__uuidof (Parameter));

Mp_devid.createinstance (__uuidof (Parameter));

Mp_trandate.createinstance (__uuidof (Parameter));

Mp_trantime.createinstance (__uuidof (Parameter));

Mp_retcode.createinstance (__uuidof (Parameter));

Mp_bustype.createinstance (__uuidof (Parameter));

Mp_cardno.createinstance (__uuidof (Parameter));

Mp_idcard.createinstance (__uuidof (Parameter));

Mp_name.createinstance (__uuidof (Parameter));

Mp_amt.createinstance (__uuidof (Parameter));

Mp_notes.createinstance (__uuidof (Parameter));

Mp_serid.createinstance (__uuidof (Parameter));

Mp_descont.createinstance (__uuidof (Parameter));

Mp_tranflag.createinstance (__uuidof (Parameter));

Mp_bankcardno.createinstance (__uuidof (Parameter));

Mp_bankgroupno.createinstance (__uuidof (Parameter));

Mp_bankflowno.createinstance (__uuidof (Parameter));

Mp_devname.createinstance (__uuidof (Parameter));

Mp_execount.createinstance (__uuidof (Parameter));

2.2.3.2 append a database parameter to the end of a parameter property of a command variable handle

Append parameterptr set to _commandptr parameters lists

Database parameter = command variable handle, create parameter method (_bstr_t ("stored procedure parameter name"), stored procedure parameter data type, stored procedure parameter input output type, default size, variable parameter value)

Mp_dotype=m_pcommand->createparameter (_bstr_t ("Dotype"), Adinteger,adparaminput,-1,vv_dotype); //integer type

M_pcommand->parameters->append (Mp_dotype);


Mp_tranno=m_pcommand->createparameter (_bstr_t ("Tranno"), Advarchar,adparaminput,32,vv_tranno); String type

M_pcommand->parameters->append (Mp_tranno);


Mp_devid=m_pcommand->createparameter (_bstr_t ("DevID"), Advarchar,adparaminput,20,vv_devid);

M_pcommand->parameters->append (Mp_devid);


Mp_trandate=m_pcommand->createparameter (_bstr_t ("Trandate"), advarchar,adparaminput,25,vv_trandate);//Time type

M_pcommand->parameters->append (mp_trandate);


Mp_trantime=m_pcommand->createparameter (_bstr_t ("Trantime"), advarchar,adparaminput,25,vv_trantime);

M_pcommand->parameters->append (Mp_trantime);


Mp_retcode=m_pcommand->createparameter (_bstr_t ("RetCode"), Advarchar,adparaminput,20,vv_retcode);

M_pcommand->parameters->append (Mp_retcode);


Mp_bustype=m_pcommand->createparameter (_bstr_t ("BusType"), Adinteger,adparaminput,-1,vv_bustype);

M_pcommand->parameters->append (Mp_bustype);


Mp_cardno=m_pcommand->createparameter (_bstr_t ("Cardno"), Advarchar,adparaminput,20,vv_cardno);

M_pcommand->parameters->append (Mp_cardno);


Mp_idcard=m_pcommand->createparameter (_bstr_t ("Idcard"), Advarchar,adparaminput,20,vv_idcard);

M_pcommand->parameters->append (Mp_idcard);

Mp_name=m_pcommand->createparameter (_bstr_t ("Name"), advarchar,adparaminput,10,vv_name);

M_pcommand->parameters->append (Mp_name);


Mp_amt=m_pcommand->createparameter (_bstr_t ("Amt"), Addecimal,adparaminput,10,vv_amt);//floating-point type

Mp_amt->numericscale = 2; Set the number of digits after the decimal point

Mp_amt->precision = 10; Set the number of integer bits

M_pcommand->parameters->append (Mp_amt);


Mp_notes=m_pcommand->createparameter (_bstr_t ("Notes"), advarchar,adparaminput,2000,vv_notes);

M_pcommand->parameters->append (mp_notes);


Mp_serid=m_pcommand->createparameter (_bstr_t ("Serid"), Advarchar,adparaminput,20,vv_serid);

M_pcommand->parameters->append (Mp_serid);


Mp_descont=m_pcommand->createparameter (_bstr_t ("Descont"), Advarchar,adparaminput,2000,vv_descont);

M_pcommand->parameters->append (Mp_descont);


Mp_tranflag=m_pcommand->createparameter (_bstr_t ("Tranflag"), Adinteger,adparaminput,-1,vv_tranflag);

M_pcommand->parameters->append (Mp_tranflag);

Mp_bankcardno=m_pcommand->createparameter (_bstr_t ("Bankcardno"), Advarchar,adparaminput,25,vv_bankcardno);

M_pcommand->parameters->append (Mp_bankcardno);


Mp_bankgroupno=m_pcommand->createparameter (_bstr_t ("Bankgroupno"), Advarchar,adparaminput,12,vv_bankgroupno) ;

M_pcommand->parameters->append (Mp_bankgroupno);


Mp_bankflowno=m_pcommand->createparameter (_bstr_t ("Bankflowno"), Advarchar,adparaminput,12,vv_bankflowno);

M_pcommand->parameters->append (MP_BANKFLOWNO);

Mp_devname=m_pcommand->createparameter (_bstr_t ("Devname"), advarchar,adparaminput,40,vv_devname);

M_pcommand->parameters->append (Mp_devname);


Mp_execount=m_pcommand->createparameter (_bstr_t ("Execount"), adinteger,adparamoutput,-1,vv_execount);//Output type

M_pcommand->parameters->append (Mp_execount);

2.2.4 execution to get results

_variant_t vnull;

VNULL.VT = Vt_error;

Vnull.scode = Disp_e_paramnotfound;

2.2.4.1 executing a database stored procedure

M_precordset = M_pcommand->execute (&vnull,&vnull,adcmdstoredproc);//adcmdstoredproc do not change

M_precordset Execute SQL Results to determine success, cause of error

if (Mp_execount->value! = 0)

{

Ct. Savelog ("Insert data Failed", 0, "");

}

Else

{

Ct. Savelog ("Insert data Success", 0, "");

}

}

catch (_com_error &err)

{

if (pmyconnect->state) pmyconnect->close ();

Pmyconnect = NULL;

CoUninitialize ();


}


2.2.5 Close Connection Release resources

if (pmyconnect->state) pmyconnect->close ();

Pmyconnect = NULL;

CoUninitialize (); Remember that success or not is called to release resources


return;


}


This article is from the "Poetic Life" blog, please be sure to keep this source http://whish.blog.51cto.com/10679902/1896409

C + + operates on database through ADO

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.