Stdafx. h
-----------------------
// Stdafx. h: Include files in the standard system, // or include files that are frequently used but not frequently changed // include files specific to the project // # pragma once # include "targetver. H "# include <stdio. h> # include <tchar. h> # include <fstream> # include <iostream> # include <ole2.h> // # include <stdio. h> # include <conio. h> using namespace STD; // todo: ReferenceProgramOther required header files/C ++ reference the ADO library method // reference the ADO library // import the ADO dynamic link library, required # import "C: \ Program Files \ common files \ System \ ADO \ msado15.dll "no_namespace Rename (" EOF "," adoeof ")
Readadovalue. h
//------------------------------
# Pragma once // # include "readadovalue. example of using the ADO library component in H "// C ++ # include" stdafx. H "// define the function void pdbvalue2 (const char * filepath); void loadcom (void); void unloadcom (void); Class readadovalue {public: readadovalue (void );~ Readadovalue (void); void pdbvalue (void );};
Readadovalue. cpp
//-------------------------
# Include "stdafx. H" # include "readadovalue. H" readadovalue: readadovalue (void) {} readadovalue ::~ Readadovalue (void) {} void readadovalue: pdbvalue (void) {pdbvalue2 ("D: \ test. mdb ");} void loadcom (void) {coinitialize (null); // initialize COM, required} void unloadcom (void) {couninitialize (); // release com} // Method for reading database table content void pdbvalue2 (const char * filepath) {coinitialize (null); // initialize COM, required // loadcom (); _ connectionptr m_pconnection; // create a smart pointer, required // m_pconnection.createinstance (_ uuidof (connection); // or m_pconnection.createins Tance ("ADODB. connection "); try {// connection string, student is the database name _ bstr_t constr; // mdb database connection string _ bstr_t mdbpath =" D: \ test. mdb "; constr =" provider = Microsoft. jet. oledb.4.0; "; constr + =" Data Source = "+ mdbpath +"; "; constr + =" User ID = admin; "; constr + =" Password = ;"; constr + = "Persist Security info = true;"; // SQL Server database connection string // constr = "provider = sqloledb.1; Password = sa; persist Security info = true; user ID = sa; initi Al catalog = student; Data Source = HSG \ HSG "; // Oracle database connection string // method 1 m_pconnection-> connectionstring = constr; cout <constr <Endl; // call the open method // m_pconnection-> open ("", admodeunknown ); // 1 // m_pconnection-> open (constr, "", "", admodeunknown); // 2 m_pconnection-> open (constr, "", "", adconnectunspecified ); cout <"database initialization successful! "<Endl;} catch (_ com_error & E) // catch connection exception {cout <" database initialization error! "<Endl; const tchar * MSG; MSG = E. errormessage (); // _ T ("error description ="); cout <"errormessage =" <MSG <Endl; // return ;} cout <"connection successful! "<Endl; _ recordsetptr PRST (_ uuidof (recordset); PRST = m_pconnection-> execute (" select * from student ", null, ad1_text ); // use the execute method, and student is the table name if (! PRST-> adoeof) {PRST-> movefirst ();} else {cout <"table data is blank" <Endl; return ;} // read the fields in the database and add _ variant_t var; string strname; // cstring strname; // lpctstr strname; char * line; while (! PRST-> adoeof) {Var = PRST-> getcollect ("stdname"); // Var = PRST-> getfields ()-> getitem ("stdname ") -> getvalue (); If (var. VT! = Vt_null) {strname = (lpcstr) _ bstr_t (VAR); // strname = (char *) _ bstr_t (VAR);} line = (char *) strname. c_str (); // Str. c_str () cout <line <Endl; PRST-> movenext ();} Try {// close PRST-> close (); m_pconnection-> close (); // release PRST. release (); m_pconnection-> release ();} catch (_ com_error & R) {} couninitialize (); // release com // unloadcom ();}