Access to SQL Server 2000 with C ++

xiaoxiao2021-03-05  21

I. 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 it is not easy to use. It is not lost to C uses databases to quickly develop Nice choice. If the theory doesn't have to spend this, there are many online, but the light is theory, nor enough, the ADO access data is very flexible, it is easy to confuse. Most of the online examples are MFC, the database is also Access Many, here I wrote a C language to access the MS SQL2000 instance, I hope to make a comparison of the rookie of my rookie. 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:

Snum snamesagessexsmajor 200113801 本拉 登 登 23 Male Computer Science 200104205 Zhang Qiaoqiao 25 ​​Women's 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] 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, for access ADO Interface prepare _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 = '本拉 登' "; try {// Create a Connection object m_pconnection.createInstance (" AdoDb.connection "); // Setting 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 in the network, Server is like (192.168.1.5, 3340) / / User SA and Password 123 is just for my library m_pconnection-> open (strconnect, ",", ", admodeunknown); if (m_pconnection == null) CERR <<" Lind Data Error! / N "; // Create a record set object m_pRecordset.CreateInstance (__ uuidof (Recordset)); // record acquired m_pRecordset-> Open (bstrSQL, m_pConnection.GetInterfacePtr (), adOpenDynamic, adLockOptimistic, adCmdText) table; _variant_t vsnum, vsname, vsage, vssex, vsmajor; // Snum, SNAME, SAGE, SSEX, SMAJOR COUT in the corresponding library, "Study number name age name, COUT <<"

/ N ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------- / N "; While (! m_precordset-> endoffile) {vsnum = m_precordset-> getCollect (_variant_t (long) 0)); // This gives a field Number and field name 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) << (_ 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 the next record} cout << "/ n -------------------------- ------------------------------------ / n "; cout <<" / n Please enter Student information / n "you want to add; cout <<": "; cin >> student.snum; cout <<" / n Name: "; cin >> Tudent.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 ();

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

New Post(0)