Consistent Data Access Technology ADO / OLE DB Microsoft Newly launched UDA (Universal Data Access, Consistent Data Access Technology) provides a consistent access interface for relational or non-relational data access, providing a multi-layer software structure for enterprise intranet Data interface standard. Consistent data access includes two-layer software interface, ADO (Active Data Object) and OLED B, respectively, corresponding to different levels of application development, ADO provides high-level software interface, can be in various scripting languages (script) or some macro language Direct use; OLE DB provides the underlying software interface, can be used directly in the C / C language. The ADO is based on OL E DB, which is packaged in OLE DB. Consistent Data Access Technology is based on Microsoft's COM (Component Object Model), which includes a set of COM component programs, components and components, or communication between components and client programs, by standard COM interfaces. Since ADO is based on automation, ADO's application is very broad, not only in the high-level language development environment such as Visual Basic, but also in some scripting languages, this for development web applications, in ASP (Active Server Page) Scripting Code Access Database provides shortcuts for operation applications. This article will first introduce the structural model of ADO and OLE DB, so that the reader understands the relationship between ADO and OLE DB, then introduces ADO's object model and some features of ADO, and we will also show the reader to readers through some examples. . I. Consistent Data Access Introduction With the continuous development of network technology and database technology, the current application system is increasingly demanding data integration, which may be distributed in different places, and use different formats, such as relationships. Documents, spreadsheets, emails, multimedia data, and directory service information, etc. in the database and operating system. Traditional solutions are to use large database systems to move all of these data to the database system, and then access these data according to the way of managing the database, so that the data can be made in a unified manner, but This indirect access has brought a lot of problems, such as data updates, not timely, space resource redundancy and access efficiency, etc. The consistent data access technology introduced by Microsoft has better solved these problems, which allows applications to access a wide variety of data through the consistent interface, regardless of where the data is resident, and does not need to perform data transfer or replication. , Conversion, also brings high efficiency while achieving distribution. And the UDA technology is in the unified data access interface, its multi-layer structure makes the data user have more choices, and its powerful extension capabilities have left more expansions for the data provider, this The open software structure makes it a strong vitality, so this technology has been widely welcomed from a launch. It can be said that UDA technology is a leap in another data access technology after ODBC. UDA technology includes OLE DB and ADO two-layer standard interface. OLE DB is a system-level programming interface that defines a set of COM interfaces. This set of interfaces encapsulates access to various data systems. This set of interfaces are data user and The data provider establishes a standard. OLE DB provides a set of standard service components for providing query, cache, data update, transaction processing, etc., so data providers only need to implement some simple data operations, in use All data control capabilities can be obtained. ADO is the application layer programming interface, which accesses data via the COM interface provided by OLE DB, which is suitable for various client / server applications and web-based applications, especially in some scripting languages, access database operations is the main advantage of ADO. . ADO is an object hierarchy established with automated technology. It has better flexibility than some other object models such as DAO (Data Access Object), RDO (Remote Data Object), etc., which is more convenient, and access data Higher efficiency. Figure 1 shows a software hierarchy model for unified data access. Figure 1 UDA hierarchical diagram we can see from the figure, the application can access data directly through the OLE DB via the ADO access data, while ADO accesss the underlying data by OLE DB. Moreover, OLE DB is divided into two parts, part of the data provider, including some basic functions, such as acquiring data, modifying data, adding data items, etc., another part is provided, including some advanced services, such as cursor functions, distributed queries and many more.
Such hierarchies provide a variety of options for data users, but also simplify the implementation of service functions for the data provider. It only needs to write a COM component program by pressing the OLED B specification, making third parties. Publishing data is easier, and in the application side can get a comprehensive functional service, this fully reflects the advantages of the OLE DB two-layer structure. Since OLE DB and ADO are implemented in the form of COM components, the various features of COM components also make it more flexible to build data applications, not just a general client / server or web application model, which is suitable for Data-centric applications are also suitable for distributed software systems for multi-layer structures. Through the configuration of COM components, we can build a variety of complex application systems. With the transparent technology from COM to DCOM, we can easily establish a distributed application system; use the MTS (Microsoft Transaction Server) to run the environment, we can also increase security control in data access, and use MTS object management Higher data access efficiency. All of these features require data providers to write code implementations, just perform regular configurations in the DCOM or MTS environment. The core of which can be said consistent data access technology is OLE DB, OLE DB establishes a standard interface for data access, which transfers all data sources through the concept of a row set (ROWSET). The OLE DB model mainly includes some of the following COM objects: (1) Data Source object data source object corresponds to a data provider, which is responsible for managing user rights, establishing initial operations such as connection with data sources. (2) Session (session) The session object is established on the basis of a data source connection, and the session object provides a transaction control mechanism. (3) Command (Command) Object Data users use command objects to perform various data operations, such as query commands, modify commands, etc. (4) Rowset object provides abstract representation of data, which can be the result of the command execution, or can be generated directly by the session object, which is the main operation object of the application. OLE DB's object model is very simple. This simplicity also brings flexibility, which can also be seen from several COM objects. Below we will start from the application layer, by establishing an ADO object model structure based on OLE DB, to help readers further understand the consistent data access technology. Second, the ADO object model ADO object model defines a set of programmable automation objects that can be used for Visual Basic, Visual C , Java, and other scripting languages that support automation features. The ADO is first used in Microsoft Internet IN Formation Server to access the database interface, and ADO can be better used in network environments compared to a general database interface, and it reduces network traffic as much as possible by optimizing technology; another of ADO Features are simple, not only because it is a database interface for advanced users, but also because it uses a set of simplified interfaces to handle various data sources. These two characteristics make ADO will replace RDO and DAO to become the final application layer data interface standard. From Figure 1, we also see that the ADO is actually an application layer interface of OLE DB. This structure also provides good scalability for the consistent data access interface, and is no longer limited to a specific data source, so ADO can Handle data sources supported by various OLE DBs. Figure 2 is an object model diagram of ADO. Figure 2 ADO object model In the ADO model, the main object has only 3: Connection, Command and Recordset, other 4 collection object errors, protes, parameters, and fields respectively correspond to Error, Property, Parameter, and Field objects, the entire ADO object model These objects are composed. A typical ADO application uses the Connection object to establish a connection with the data source, and then give a command to the database operation with a Command object, such as query or update data, and the RECORDSET is used to maintain or browse the result set data. The command language used by the command command is related to the OLE DB data source corresponding to the underlying, and different data sources can use different command languages, for relational databases, usually use SQL as a command language. In the 3 objects of Connection, Command, and Recordset, the Command object is an optional object that is effective dependent on whether the OLE DB data provider implements the ICommand interface.
Since the OLE DB can provide a relational data source, you can also provide a non-relational data source, so use a traditional SQL command query data on non-relational data sources, and even the Command command object cannot be used. From the structure, the ADO model is very simple, but it is very flexible, let's discuss from the perspective of a single object: (1) Connection object Connection object represents a connection between the data source, ADO's Connec Tion object Encapsate the data source objects and session objects of the OLE DB. According to the different performance of the OLE DB provider, the characteristics of the Conne Cstion object are also different, so the methods and attributes of the Connection object are not necessarily available. With the Connection object, we can complete some basic settings. A. Set the connection string, timeout information, access mode via Connectionstring, ConnectionTimeout, and Mode properties. B. You can also set the CURSORLocation property to specify the client cursor to use the batch modification method in the client program. C. Set the default database properties of the connection DEFAULTDATABASE. D. Set the properties provike of the OLE DB provider. E. Configure the connection of the Connection object with the physical data source via the Open and Close. F. Execute the command via the Execute method. G. Provide transaction mechanisms, implement transaction control through beginTrans, CommitTrans, and RollbackTrans methods. H. Check the error message of the data source through the ERRORS collection attribute. I. Get table information of the database through the OpenSchema method. The Connection object is one of the basic objects of ADO, which is independent of all other objects. If we want to query the database, we can use the Execute method or use a Command object. Use the Execute method to be relatively easy, but with the Command object to save the command information so that you can query multiple times. (2) Command object Command object represents a command, which can perform information about the data source, such as query, modification, etc. through its method. The usage of the Command object is as follows: a. Set the command string through the CommandText property. B. Define parameters for parametric queries or stored procedures through Parameters collection properties and Parameter objects. C. Run the command, possible words through the Execute method, return to the Recordset object. D. Before executing the command, you can set the CommandType property to optimize performance. E. You can indicate a compiled version to the current command through the pre previous property, and it will be greatly accelerated when executed. F. The timeout value (in seconds) performed by the CommandTimeOut property setting command is set. G. You can set the ActiveConnection property to specify the connection string for the command, and the Command object will create a C Onnection object internally. H. You can set the Name property so that you can execute the method name specified by the Name property on the corresponding Connection object. When the Command object is executed, you can specify the connected Connection object by the ActiveConnection property, or you can specify the connection string independently of the Connection object, even if the connection string is the same as the connection string of the Connection object, the Command object still uses its internal data source connection. (3) Recordset object RecordSet object represents a result of a table's recordset or command execution, in the recordset, there is always a current record. The record set is the basic object of the ADO management data. All RecordSet objects are managed in a row-like manner, each line corresponds to a record, and each column corresponds to a field. The Recordset object also access the record through the cursor. In ADO, the cursor is divided into the following: static cursors provide a static copy of the dataset, allowing various mobile operations, including forward, post-transfer, etc., but other users The operation made is reflected. Dynamic cursors allow various mobile operations, including forward movement, post-transfer, etc., and other users do can also be directly reflected. The forward cursor allows various forward moving operations that cannot be moved backwards, and the operations made by other users can also be reflected.
Keyset cursor is similar to a dynamic cursor, and it is also possible to see the data modifications made by other users, but cannot see other users' new records, and they cannot access records from other users. The Usage of the Recordset object is as follows: a. Use the Cursortype property to set the cursor type. b. Open the recordset data via the open method, you can assign a value to the ActiveConnection property before Open, specifying the RecordSet object, can also specify the connection string parameter directly in the open method, and the ADO will create an internal connection, even if the string is connected The external connection object is the same, and it also uses a new connection object. The C.Recordset object is just open, the current record is positioned in the first record, and the BOF and EOF flag attribute are F Alse, if the current recordset is a empty recordset, the BOF and EOF flag properties are TRUE. D. Move the cursor of the recordset by movingfirst, MoveLast, MoveNext and MovePRevious methods. If the OLE DB provider supports related features, you can use the AbsolutePosition, Absol Utepage, and Filter properties to reposition the current record. E.ado offers two record modifications: Immediate modifications and batch modifications. In immediately modifying the mode, once the u pdate method is called, all modifications to the data are immediately written to the base of the data source. In the batch modification mode, multiple records can be modified, then call the UpdateBatch method to submit all modifications to the underlying data source. After submit, you can check the data conflict with the Status property. The RecordSet object is the core of the ADO data operation. It can be used as a result of a specific method as a Connection object or a Command object, or can be used independently of these two objects, thereby seeing the flexibility of the ADO object in use. . The above three objects contain the properties of the Property object collection, which allows the ADO dynamic to dynamically expose the performance of the underlying OLE DB provider. Since all underlying providers have the same performance, ADO allows users to dynamically access the underlying provider's capabilities. This makes ADO flexible and provides good scalability. Ado's other collection objects and its element objects are used in a specific context environment, such as the Parameter object, must truly work after contact with a Command object. Other three objects Field, ERROR and Property objects can only be included in their parent objects, and these objects cannot be created separately. Third, in a variety of languages describe the ADO object model, now we discuss how to use ADO objects in different locations. Because ADO is implemented as an automation component program, we can use ADO in any locale supporting COM and automation features, such as Visual Basic, Visual C , ASP, and Java, etc., below. 1. Using the ADO Visual Basic app in the Visual Basic application, you can create and use the automation object in design mode and run mode. In design mode, the object library like ADO can be used as internal objects, we only need "" In the Project menu, the "reflected" command pops up in the dialog box to pop up the ADO object library "Microsoft ActiveX Data Objects Libra Ry", so we can declare or create an ADO object directly in the program. For example: DIM CN AS New Adodb .Connection DIM CMD AS NEW AdoDB.Command Dim RS As New AdoDB.Recordset It can be seen that using ADO objects in design is very convenient, and the object browser feature provided in the Visual Basic design environment allows users to be conveniently Check the properties and methods of the ADO object.
We can also create an automated object at runtime. Use the Visual Basic's CreateObject function to create any automation objects. Due to only Connection objects in the ADO, the Command object and the RecordSet object can be created independently, so we can only create these three objects. Examples are as follows: DIM CN SET RS = CreateObject ("AdoDb.Connection") DIM CMD SET RS = CreateObject ("AdoDb.command") DIM RS SET RS = CREATEOBJECT ("AdoDb.Recordset") Whether it is design mode or operation mode The properties and methods of calling ADO objects are very simple, and they can be called directly. 2. Using the ADO ADO object in the ASP VBScript can also be used for the VBScript script code for HTML and Active Server Page, and the VBScript script code is very similar to the Visual Basic code, which is embedded within the HTML or ASP file. However, the VB Script engine is functionally weaker than Visual Basic's design environment or run library. First, in the VBSCRI PT code, there is no similar use with the design environment, the VBScript engine cannot be loaded into the ADO type library, so you can't use New The operator creates an ADO object, but you can use the CreateObject function to create an object; secondly, the constant used in the ADO object library can only be introduced by including files, and the Adovbs.inc files provided with ADO contain all ADO constants, we can This file directly in the script code. Therefore, in order to use ADO in the VBScript code, first include an adovbs.inc file, then create an ADO object using the CREATE OBJECT function, and then call the properties or methods of these objects. The following example shows the name and title of all the authors in the data table in the ASP file, the code is as follows: USING ADO IN A Visual Basic Script Web Page Using Ado In a Visual Basic Script Web Page MyConnection.Open "DSN = Mysamples; uid = sa "sqlQuery =" select authorname, title from authordb "set = myconnection.execute (sqlQuery)% width = 200 title id size = 1 title" arial narrow "size = 1" arial narrow "size = 1 3 In Visual C , use ADO to use ADO in Visual C , the first method is to create an ADO object using the COCREATEINSTANCE function and get the object's iDispatch interface pointer, then call its Invoke function, use this method We handle parameters and return values, ADO provides adoid.h and adoint.h header files defined the CLSID and interface ID of ADO objects, respectively; the second method is to use the #import compilation indicator (after Visual C 5.0 and later You can use it in the version, which is convenient to use the ADO object; the third method is to create and call the ADO object using the IDispatch interface encapsulation class COLEDISPATCH interface package provided by the Microsoft Foundation C Lass.