C ++ access MS SQL2000 instance

xiaoxiao2021-03-06  27

First, ADO Introduction

ADO (ActiveX Data Object) is a new interface developed by Microsoft Database Application. It is a high-level database access technology built on OLE DB, not only easy to use, and does not lapse flexibility. Nice choices for fast development of databases with C . Theory If I don't have to spend this, there are many online, but the light is theory, nor enough, ADO access data is flexible, it is easy to confuse. Most of the online examples are based on MFC, and the database is also access. Here I wrote a C language to access the MS SQL2000 instance, I hope that the rookie of my rookie is helpful. Second, the establishment of the library first creates a database Student in the SQL2000 Enterprise Management, and creates a table stu_info field and the value as follows:

Snumsnamesagessexsmajor200113801 本拉 登 23 Male Computer Science 200104205 Zhang Qiaoqiao 25 ​​Female Tourism Management 200113802 Jacky Cheung 26 Male Computer Science

Third, the list of access procedures is as follows: (Win2000 VC6.0) / ****************************************** ******************************** Use ADO to access MS SQL2000 requirements: [1] Output STU_INFO table, each record [2] Add a new record [3] Remove the name "Ben Laden" record * / #import "c: / program files / common files / system / ado / msado15.dll" / no_namespace rename ("eof", " Endoffile ") # include #include // for setw () Using namespace std; class stu {public: char snum [10]; // 学 号 CHAR SNAME [10]; // Name Char SSEX [ 2]; // Name 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 the ADO interface_RecordSetPtr M_PRecordset ("AdoDb.Recordset"); _ConnectionPtr M_PConnection ("AdoDb.Connection"); _bstr_t bstrSQL ( "select * from stu_info"); // query char * query_cmd = "DELETE fROM stu_info WHERE sname = 'Osama bin 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 = "proviker = SQLOLDB; server = (local); database = student; uid = sa; pwd = 123;"; // If the database is on the network The 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 the recordset object m_pRecordset.CreateInstance (__ uuidof (recordset)); // table records acquired m_pRecordset-> Open (bstrSQL, m_pConnection.GetInterfacePtr (), adOpenDynamic, adLockOptimistic, adCmdText ); _VARIANT_T VSNUM, VSNAME, VSAGE, VSSEX, VSMAJOR; // Snum, SNAME, SAGE, SSEX, SMAJOR COUT << " Name old name: "COUT <<" / n ------------------------------------- -------------------------- / N "; while (! m_precordset-> endoffile) {vsnum = m_precordset-> getCollect (_variant_t LONG) 0)); // This can be vsName = m_precordset-> getCollect ("SNAME"); vssex = 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 << setw (20) << (char *) (_ bstr_t) vsmajor; cout.unsetf (ios :: left); cout << endl;} m_precordset-> MoveNext (); // / Move to next Record} cout << "/ n ----------------------------------- -------------------------- / N "; cout <<" / n Please enter the student information you want to add; cout << "" Student No.: "; CIN >> Student.snum; COUT <<" / N Name: "CIN >> Student.sname; COUT <<" / N Age: "CIN >> Student.sage; Cout << "/ n surname:"; 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-76880.html

New Post(0)