C ++ language Access instance of MS SQL2000

xiaoxiao2021-03-06  14

/ ************************************************** ***************** 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 #include / / for setw () Using namespace std; class stu {public: char snum [10]; // student number char SNAME [10]; // Name char ssex [2]; // Sathery long Sage; // Age char Smajor [20]; // Professional public: stu () {} ~ stu () {}}

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 < mo_precordset (); /// Move to Next Record} Cout < <"/ n ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------ / n "; cout <<" / n Please enter the student information you want to add: COUT << ":"; cin >> Student.snum; cout << "/ n Name:"; cin >> student.sname; cout << "/ n age:"; cin >> student.sage; cout << "/ n ; CIN >> Student.Ssex; cout << "/ n Professional:"; cin >> student.smajor; m_precordset-> movefirst (); // Move to the first record m_precordset-> addnew (); // / Add new record m_pRecordset-> PutCollect ( "snum", _ variant_t (student.snum)); m_pRecordset-> PutCollect ( "sname", _ variant_t (student.sname)); m_pRecordset-> PutCollect ( "sage", _ variant_t (student.sage ))); M_precordset-> Putcollect ("SSEX", _ variant_t (student.ssex));

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

转载请注明原文地址:https://www.9cbs.com/read-48141.html

New Post(0)