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 ;}