C ++ reads SQL Server and SQL Server

Source: Internet
Author: User

C ++ reads SQL Server and SQL Server

// ReadSqlConsole. cpp: Master project file. # Include "stdafx. h "# include <iostream> # include <iomanip> // for setw () # include" windows. h "# import" C: \ Program Files \ Common Files \ System \ ado \ msado15.dll "no_namespace rename (" EOF "," EndOfFile ") using namespace System; using namespace std; int main (array <System: String ^> ^ args) {: CoInitialize (NULL); // type defined in msado15.dll _ RecordsetPtr m_pRecordset ("ADODB. recordset "); _ ConnectionPtr m_pConn Ection ("ADODB. connection "); _ bstr_t bstrSQL (" select * from er_order where pactnumber like '000000' order by pactnumber "); // The corresponding SQL statement try {clog <"connecting to the database... "<endl; m_pConnection.CreateInstance (" ADODB. connection "); // create a Connection object // set the Connection string, which must be BSTR or _ bstr_t strConnect =" Provider = SQLOLEDB; Server = [database instance ]; database = [Database name]; uid = [Database user]; pwd = [Password]; "; m_pConnection-> Open (strConnect ,"", "", AdModeUnknown); // server connection if (m_pConnection = NULL) {std: cerr <"Lind data ERROR! \ N ";}m_precordset.createinstance (_ uuidof (Recordset); // creates a record set object // retrieves the table record m_pRecordset-> Open (bstrSQL, explain (), adOpenDynamic, adLockOptimistic, adshorttext); _ variant_t PactNumber, OrigCount; // The Field cout in the corresponding library <"-------------- contract list ------------" <endl; while (! M_pRecordset-> EndOfFile) {PactNumber = m_pRecordset-> GetCollect ("Pactnumber"); OrigCount = m_pRecordset-> GetCollect ("Origcount"); cout <"Contract No: "<setw (10) <(char *) (_ bstr_t) PactNumber; cout <" ticket amount: "<setw (7) <(char *) (_ bstr_t) OrigCount <endl; m_pRecordset-> MoveNext (); // next record} m_pRecordset-> Close (); // Close record set} catch (_ com_error e) // catch exceptions {cerr <"\ nERROR:" <(char *) e. description ();} if (m_pConnection-> State) {m_pConnection-> Close (); }:: CoUninitialize (); Console: ReadLine (); 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.