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, not to use flexibility. Nice choices for fast development of C uses 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, create a database Student and create a table stu_info field and the value as follows:
Snum sname sage ssex smajor 200113801 本拉 登 23 Male Computer Science 200104205 Zhang Qiaoqiao 25 Women's Tourism Management 200113802 Jacky Cheung 26 Male Computer Science
Third, visit
The list of 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
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 recording m_pRecordset-> Open (bstrSQL, m_pConnection.GetInterfacePtr (), adOpenDynamic, adLockOptimistic, adCmdText); _ variant_t vsnum, vsname, vsage, vssex, vsmajor; // the corresponding library snum, sname, sage, ssex, smajor cout << "Learn to name the name of the name of the name"; 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 <
, _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); // with Execute Execute the SQL statement 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;}