Use ADO in Visual C (on) 1. Generate an application framework and initialize the OLE / COM library environment
Create a standard MFC AppWizard (EXE) application, then initialize the OLE / COM library in the initInstance function using the ADO database (because the ADO library is a COM DLL library). This example is:
Bool Cadotestdlg :: OnNitdialog () {:: Coinitialize (null); // Initializing OLE / COM Library Environment}
The program is finally called :: Couninitialize (); // Release the program occupied COM resources.
In addition:
M_PRecordset-> Close (); note! ! ! Don't close multiple times! ! ! ! ! ! ! ! ! ! ! ! m_pConnection-> close (); m_precordset = null; m_pconnection = null; 2. Introduced ADO library file
Before using ADO, you must introduce the ADO library file with the direct introduction symbol #import with the direct introduction symbol #import so that the compiler can be compiled correctly. The code is as follows: #Import "c: / program files / common files / system / ado / msado15.dll" No_Namespace Rename ") The definition of the ADO class is stored as a resource in ADO DLL (MSADO15. DLL is called a type library in it. The type library describes the autonomous interface, and the COM VTable interface used by C . When using the #import command, Visual C needs to be read from the ADO DLL when running, and create a set of C header files. These header files have .TLI and .TLH extensions, readers can find these two files in the project's directory. The ADO class called in the C program code is defined in these files. The third line of the program indicates that the ADO object does not use the namespace. In some applications, naming conflicts may occur due to objects in the application in the application, there is a need to use the namespace. If you want to use a namespace, you can modify the third line program to: Rename_NameSpace ("Adons"). The fourth line of code is renamed the EOF in the ADO (end) to AdoEOF to avoid conflicts with other library defined for your EOF.
3. Database operation using intelligent pointers
Define two ADO intelligence pointer instances in the CaboutDLG header and add a listCtrl in the dialog.
Class Cadotestdlg: Public CDialog {_ConnectionPtr M_PCONNECTION; _RecordSetPtr M_PRecordset; ClistCtrl M_List; ......}
The ADO library contains three intelligent pointers: _ConnectionPtr, _Commandptr, and _recordsetptr.
_ConnectionPtr is usually used to create a data connection or perform a SQL statement that does not return any result, such as a stored procedure. _Commandptr returns a recordset. It provides a simple way to perform the stored procedures and SQL statements that returns the record set. When using the _CommandPTR interface, you can use the global _connectionptr interface, or you can use the connection string directly in the _commandptr interface. _RecordSetPtr is a recordset object. Compared with the above two objects, it provides more control functions to records, such as record lock, cursor control, and the like. Inbutton1 Add the following code in the event response using the ADO program:
void CAdotestDlg :: OnButton1 () {m_List.ResetContent (); m_pConnection.CreateInstance (_uuidof (Connection)); // initialize pointer Connection m_pRecordset.CreateInstance (_uuidof (Recordset)); // initialize pointer Recordset try {m_pConnection-> Open ("DSN = Adotest", "," ", 0); // Connection is an ODBC data source // read: This is an open function of the connection that does not require a user ID or password, otherwise the form is -> Open ("DSN = TEST; UID = SA; PWD = 123;", ",", ", 0); // Execute SQL statement to get a recordset to assign its pointer to m_precordset cstring strsql =" select * from middle "; BSTR bstrSQL = strSql.AllocSysString (); m_pRecordset-> Open (bstrSQL, (IDispatch *) m_pConnection, adOpenDynamic, adLockOptimistic, adCmdText); // adOpenDynamic:! adLockOptimistic optimistic blockade dynamic method adCmdText: text query while (m_pRecordset-> adoEOF ) // Traverse all records {// Take a record field value method _variant_t thevalue; // variant data type Thevalue = m_pRecordset-> getCollect ("BIG_NAME"); // Get the value of the field BIG_NAME (thevalue.vt! = Vt_null) m_list.addstring ((char *) _ bstr_t (thevalue))); // Add this value to list control // _BSTR_T thevalue1 = m_precordset-> fields-> GetItem ("BIG_NAME ") -> value; // cstring temp = thevalue1.copy (); // m_list.addstring (Temp); // Data Type Conversion _Variant_t Vusername, Vbirthday, VID, VOLD; Trace ("ID:% D, Name:% s, Age:% D, Birthday:% S / R / N" , Vid.lval, (lpctstr) (_ BSTR_T) VUSERNAME, VOLD.LVAL, (LPCTSTSTSTR) (_ bstr_t) vbirthday;
m_precordset-> MoveNext (); // Go to Next Record} m_precordset-> close (); m_pconnection-> close ();} catch (_ERROR E) // Expedition {AFXMESSAGEBOX (E.ERRORMESSAGE ());} M_PRecordset-> Close (); // Note! ! ! Don't close multiple times! ! ! ! Otherwise, you will erronely M_PRecordSet = NULL; m_pconnection = null;} The program is converted by _variant_t and _bstr_t to convert COM objects and C type data, and _variant_t classes encapsulate OLE Autonomous Variant data types. Using the _variant_t class in C is much easier than using the Variant data type.
Ok, after compiling the program runs, remember to create an ODBC data source called Adotest before running. The program will display the BIG_NAME field value in the table Middle in the list control.