Access to SQL Server 2000 with C ++

xiaoxiao2021-03-06  39

Accessing SQL Server 2000 with C , ADO Introduction is the new interface developed by Microsoft Database Application, which is a high-level database access technology built on OLE DB, not only easy to use, and does not indiferous Nice choices for fast development of C using databases. 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 in the SQL2000 Enterprise Management, establishes a database Student, and creates a table stu_info field and the value as follows: Snum sname Sage ssex smajor 200113801 本拉 登 23 Male Computer Science 200104205 Zhang Qiaoqiao 25 ​​female tourism management 200113802 Jacky Cheung 26 male Computer Science III, the list of access procedures is as follows: (Win2000 VC6.0) / ********************************* **************************************** Use ADO to access MS SQL2000 requirements: [1] Output STU_INFO table A record [2] Add a new record [3] Delete the name "Ben Laden" record * / #import "c: / program files / common files / system / ado / msado15.dll" / no_namespace rename ("eof" , "Endoffile" # d #include // for setw () Using namespace std; class stu {public: char snum [10]; // student number char SNAME [10]; // Name char SSEX [2]; // Satarment long Sage; // Age Char Smajor [20]; // Professional public: stu () {} ~ stu () {}}; int main () {stu student; :: Coinitialize (NULL); // Initialize the OLE / COM library environment, prepare _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 Connection object M_PCONN ECTION.CreateInstance ("AdoDb.Connection"); // Set the connection string, must be a BSTR type or _bstr_t type _BSTR_T strconnect = "provike = sqloledb; 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 record m_precordset->

Open (bstrSQL, m_pConnection.GetInterfacePtr (), adOpenDynamic, adLockOptimistic, adCmdText); _variant_t vsnum, vsname, vsage, vssex, vsmajor; // correspond library snum, sname, sage, ssex, smajor cout << "Student ID Name Age surname professional "; 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"); vssex = m_precordset-> getCollect ("ssex" ); Vsmajor = m_precordset-> getCollect ("smajor"); if (vsnum.vt! = Vt_null && vsname.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_prec ORDSET-> MoveNext (); /// Move to Next Record} Cout << "/ N ---------------------------- ---------------------------------- / N "; cout <<" / n Please enter you want Added Student Information / N "; COUT <<": "; cin >> student.snum; cout <<" / n Name: "; cin >> student.sname; cout <<" / N age: " Cin>

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

New Post(0)