C ++ calls ADO to access SQL 2000

Source: Internet
Author: User
Code
// Adotest. cpp: Define console applicationsProgram.
//

# Include " Stdafx. h "
# Import " C: \ windows \ system32 \ msado15.dll " No_namespace Rename ( " EOF " , " Adoeof " )

Int _ Tmain ( Int Argc, _ tchar * Argv [])
{

: Coinitialize (null );
_ Connectionptr m_pconnection;
Hresult HR = M_pconnection.createinstance (_ uuidof (connection ));
If ( ! Succeeded (HR ))
{
STD: cout < " M_pconnection.createinstance (_ uuidof (connection) Error " < STD: Endl;
Sleep ( - 1 );
Return   0 ;
}
STD :: String Str =   " Provider = sqloledb.1; Password = sa; persist Security info = true; user id = sa; initial catalog = success; Data Source = WUBIYU-PC " ;
M_pconnection -> Connectionstring = " Provider = sqloledb.1; Password = sa; persist Security info = true; user id = sa; initial catalog = success; Data Source = WUBIYU-PC " ;

Try
{
M_pconnection -> Open (_ bstr_t) Str. c_str (), "" , "" , Null );
}
Catch (_ Com_error & E)
{
STD: cout < " Opening failed: " < E. Description () < STD: Endl;
Sleep ( - 1 );
Return   0 ;
}

_ Recordsetptr recordsetp;

If (Failed (recordsetp. createinstance (_ uuidof (recordset ))))
{
STD: cout < " Failed to create recordset object: " < STD: Endl;
Sleep ( - 1 );
Return   0 ;
}
Try
{
// Recordsetp-> open (_ variant_t ("sell_agent "),
// _ Variant_t (idispatch *) m_pconnection, true), cursortypeenum: adopenkeyset, locktypeenum: adlockoptimistic, commandtypeenum: adcmdtable );
// Recordsetp-> open (_ variant_t ("sell_agent "),
// _ Variant_t (idispatch *) m_pconnection, true),: adopenkeyset,: adlockoptimistic,: adcmdtable );

}
Catch (_ Com_error & E)
{
STD: cout < " Opening failed " < E. Description () < STD: Endl;
}

// STD: String sorted STR = "Update sell_agent set integral = 21 where id = 2 ";
STD :: String Reverse Str =   " Select * From sell_agent where id = 2 " ;
_ Commandptr pcmdchange;
HR = Pcmdchange. createinstance (_ uuidof (command ));
If ( ! Succeeded (HR ))
{
STD: cout < " CREATE command object Error " < STD: Endl;
Sleep ( - 1 );
Return   0 ;
}
Pcmdchange -> Activeconnection = M_pconnection;
Pcmdchange -> Commandtext = (_ Bstr_t) Comment Str. c_str ();
M_pconnection -> Errors -> Clear ();
_ Variant_t recordscount = 0 ;
Recordsetp = Pcmdchange -> Execute ( & Recordscount, null, ad1_text );
STD: cout < ( Long ) Recordscount < STD: Endl;

Try
{
STD: cout < Recordsetp -> Getfields () -> Count < STD: Endl;
Recordsetp -> Movefirst ();
While ( ! Recordsetp -> Adoeof)
{
Int Id_value = ( Int ) Recordsetp -> Fields -> Item [_ variant_t ( " ID " )] -> Value;
STD :: String ID = (_ Bstr_t) recordsetp -> Getfields () -> Getitem (( Long ) 0 ) -> Name;
STD :: String Name_value = (_ Bstr_t) recordsetp -> Getfields () -> Getitem (( Long ) 1 ) -> Value;
STD :: String Name = (_ Bstr_t) recordsetp -> Getfields () -> Getitem (( Long ) 1 ) -> Name;
STD: cout < Id. c_str () < " : " < Id_value < " " < Name. c_str () < " : " < Name_value.c_str () < Endl;
Recordsetp -> Movenext ();
}
}
Catch (_ Com_error & El)
{
STD: cout < El. Description () < STD: Endl;
}
STD: cout < " Complete " < STD: Endl;
: Couninitialize ();
Sleep ( - 1 );
Return   0 ;
}

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.