VC uses ADO Access Database Raising 攻 攻 作者 作者 生 生 昌 昌 作者 作者 作者>>>> 攻 攻 攻 攻 攻 攻 攻 攻 攻 攻 攻 攻 攻 攻ADO Access Database Raiders, introduces VCs to access the database and various methods of accessing the database, very classic, very practical, very worthwhile. Text 1, ADO Overview ADO is Microsoft designed for the latest and most powerful data access example OLE DB, is an application layer interface that is easy to use. ADO allows you to write applications to access and operate data in the database server via OLE. DB provider. The most important advantages of ADO are easy to use, fast, and less memory spending and small disk remains. ADO uses minimal network traffic in a critical application, and uses the least number of layers between the front and data sources, all of which are to provide lightweight, high-performance interfaces. The reason is called ADO, which has used a familiar metaphor, OLE automation interface. OLE DB is a set of "Component Object Model" (COM) interfaces, a new database low-level interface, which encapsulates the function of ODBC and access data stored in different information in a unified manner. OLE DB is the technical foundation of the Microsoft UDA policy. OLE DB provides high performance access for any data source, including relationships and non-relational databases, email, and file systems, text, and graphics, custom business objects, and more. That is, OLE DB is not limited to ISAM, JET, even relational data sources, which can handle any type of data regardless of their format and storage methods. In practical applications, this diversity means accessing data residing in an Excel electronic data sheet, a text file, an email / directory service, and even a mail server, such as Microsoft Exchange. However, the purpose of the OLE DB application programming interface is to provide the best features for a variety of applications, which do not meet simplified requirements. The API you need should be a bridge that connects to applications and OLE DB, which is ActiveX Data Objects (ADO). Second, using the ADO in the VC (the development step is good :) 1, before introducing the ADO library file Using ADO to introduce the STDAFX.H header file to introduce the ADO library file with the direct introduction symbol #import in the project, so that the compiler can be correct Compilation. The code is as follows: introducing the ADO library file with #import #import "c: / program files / common files / system / ado / msado15.dll" NO_NAMESPACES RENAME ("EOF" AdoEOF ") Declaration of ADO in the project However, do not use the namespace of ADO, and to avoid constant conflicts, the constant EOF is renamed as AdoEOF. Now you need to add additional header files, you can use the ADO interface. 2, the initialization OLE / COM library environment must be noted The ADO library is a set of COM dynamics, which means that the application must initialize the OLE / COM library environment before calling ADO. In the MFC application, a better method is initialized in the initInstance member function of the application main class. OLE / COM library environment.
Bool CMYADOTESTAPPP :: InitInstance () {if (! Afxoleinit ()) // This is initializing the COM library {AFXMessageBox ("OLE initialization error!"); Return false;} ...} 3, ADO interface Introduction ADO library contains three Basic interface: _ConnectionPTR interface, _commandptr interface and _RecordSetPtr interface. _ConnectionPTR interface returns a recordset or an empty pointer. It is usually used to create a data connection or perform a SQL statement that does not return any result, such as a stored procedure. Returning a recordset using the _ConnectionPTR interface is not a good method. For operations to return records, it is usually implemented with _RecordSerptr. When using _ConnectionPTR operation, you should want to get the number of records to traverse all records, and use _RecordSerptr. _CommandPTR interface 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. If you only perform one or more data access, the latter is a better choice. But if you want to access the database frequently and return a lot of records, you should use the global _connectionptr interface to create a data connection, then use the _CommandPTR interface to perform the stored procedure and SQL statements. _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. Like the _CommandPTR interface, it does not have to use a data connection that has been created, you can use a connection string instead of the Connection member variable assigned to _RecordSetptr, let it create data connections yourself. If you want to use multiple record sets, the best way is to use the Command object to use the global _connectionptr interface that has created a data connection, and then use the _recordsetPTR to perform the stored procedure and SQL statements. 4, use _ConnectionPTR interface _ConnectionPTR is mainly a connection interface, acquiring connection with the database. Its connection string can be written directly, or you can point to an ODBC DSN. .
_ConnectionPtr pConn; if (FAILED (pConn.CreateInstance ( "ADODB.Connection"))) {AfxMessageBox ( "Create Instance failed!"); Return;} CString strSRC; strSRC = "Driver = SQL Server; Server ="; strSRC = "suppers0"; strsrc = "; databaserc =" MYDB "; strsrc ="; uid = sa; pwd = "; cstring strsql =" Insert Into Student (No, Name, SEX, Address) Values (3, 'AAA', 'Male', 'Beijing' "; _ Variant_t Varsrc (strsrc); _ variant_t varsql (strsql); _ bstr_t bstrsrc (strsrc); if (failed (pconn-> open (bstrsrc,", "," 1))) {AFXMESSAGEBOX ("Can NOT OPEN DATABASE!"); PConn.Release (); Return;} Colevariant VtOption ((long) DISP_E_PARAMNOTFOTION, VT_ERROR); PCONN-> EXECUTE (_BSTR_T (STRSQL), & VtOptional, -1 PConn.release (); AFXMessageBox ("OK!"); 5, use the _recordsetPtr interface (to connect SQL Server as an example) _recordsetptr pptr; if (pptr.createinstance ("adoDb.recordset"))) { AfxMessageBox ( "Create Instance failed!"); return FALSE;} CString strSRC; strSRC = "Driver = SQL Server; Server ="; strSRC = "210.46.141.145"; strSRC = "; Database ="; strSRC = "mydb" ; strsrc = "; uid = sa; pwd ="; strsrc = "sa"; cstring strsql = "Select id, name, gender, address from personal"; _ variant_t varSRC (strSRC); _ variant_t varSQL (strSQL); if (FAILED (pPtr-> Open (varSQL, varSRC, adOpenStatic, adLockOptimistic, adCmdText))) {AfxMessageBox ( " Open table failed! "); pPtr.Release (); return FALSE;} while (pPtr-> GetadoEOF (!)) {_ variant_t varNo; _variant_t varName; _variant_t varSex; _variant_t varAddress; varNo = pPtr-> GetCollect (" id " VarName =