/ ************************************************** ***************** Use ADO to access MS SQL2000 requirements: [1] Output STU_INFO table, each record [2] Add a new record [3] Delete name " Ben Laden "record * / #import" c: / program files / compon files / system / ado / msado15.dll "/ no_namespace rename (" eof "," endoffile ") # include
INT main () {stu student ;:: Coinitialize (null); // Initialize the OLE / COM library environment, prepare for accessing ADO interface
_RecordSetPtr M_PRecordset ("AdoDb.Recordset"); _ConnectionPtr M_PConnection ("AdoDb.Connection");
_BSTR_T BSTRSQL ("Select * from stu_info"); // Query statement char * query_cmd = "delete from stu_info where sname = 'Ben Laden'";
Try {// Create a Connection object m_pconnection.createInstance ("AdoDb.Connection"); // Set the connection string, must be a BSTR type or _bstr_t type _BSTR_T strconnect = "provike = SQLOLDB; Server = (local); dataBase = STUDENT; UID = SA; PWD = 123; "; // If the database is on the network, Server is like (192.168.1.5, 3340) // User SA and password 123 just for my library m_pconnection-> open (StrConnect, "", ",", admodeunknown; if (m_pconnection == null) CERR << "Lind Data Error! / N"; // Create record set object m_precordset.createInstance (__ uuidof (recordset)); // Get the table Record m_precordset-> Open (BSTRSQL, M_PConnection.GetInterfacePtr (), AdoPENDYNAMIC, AdlockOptimistic, AdcmdText);
_VARIANT_T VSNUM, VSNAME, VSAGE, VSSEX, VSMAJOR; // Snum, SNAME, SAGE, SSEX, SMAJOR COUT << "Smajor Cout" in the corresponding library; COUT << "/ n ----- -------------------------------------------------- --------- / n ";
While (! m_precordset-> endoffile) {vsnum = m_precordset-> getCollect (_VARIANT_T (long) 0)))); // This can be VsName = m_precordset-> getCollect ("sname"); vsage = m_precordset-> getCollect ("Sage"); vssex = m_precordset-> getCollect ("ssex"); vsmajor = m_precordset-> getCollect ("smajor"); if (vsnum.vt! = vt_null && vsname.vt! = vt_null&& vsage.vt! = vt_null && vssex.vt! = vt_null && vsmajor.vt! = vt_null) {cout.setf (ios :: left); cout << setw (14) << (char *) (_ bstr_t) vsnum; cout << SETW (14) << (char *) (_ bstr_t) vsname; cout << setw (8) << vsage.lval; cout << setw (8) << (char *) (_ BSTR_T) vSsex; cout <
M_PRecordset-> Putcollect ("smajor", _ variant_t (student.smajor)); m_precordset-> update (); m_pconnection-> execute (query_cmd, null, 1); // Execute SQL statements with Execute to delete m_precordset-> close ); // Close record set}
// Capture an abnormality catch (_COM_ERROR E) {// Display Error Message CERR <<< / Nerror: "<< (char *) E.DESCRIPTION (); // Throw anomalous} IF (m_pconnection-> state) m_pConnection- > Close ();
:: Couninitialize ();
Return 0;}