Overview
ADO objects can be used on all platforms that support COM and OLE, such as Vb,vc++,vbscrip,java and so on. Includes the following seven objects:
Connection
Command
Parameter
Recordset
Field
Property
Error
and four collections:
Fields
Properties
Parameters
Errors
The core of ADO is the Connection,recordset and command objects. First use connection to establish a connection with the server, and then use the command object to execute commands, such as queries, updates, and so on. Use the Recordset object to manipulate and view query results. It's so simple.
Of course, like its name, the Parameters collection and the Parameters object are parameter information for the command object. The error collection and errors object in connection provide the wrong information after an error has occurred. Once the query results are obtained, the field collection and Field objects can be used through the Recordset object, which provides information about the related fields.
Long March first step
Everything starts with the connection. The Connection object includes the initial and connection mechanisms, as well as executing queries, using transactions, and so on. By default, the ODBC-driven (MSDASQL) provided by MS is used. To use other vendor's dongdong, you need to set it in the Provider property. With ODBC drivers, ADO can connect to any ODBC-enabled database service: An ODBC data source or an externally specified data source information (usually a DSN-less connection).
Before you start the connection, set the connection word, default database, and connection properties. The Open method in the object is used to establish the connection. Use the Execute method to execute a query. Transaction processing can also be supported in connection objects: through Begintrans,committrans, and RollbackTrans methods.
Here is an example of connecting to SQL Server using ODBC drivers (simple!). ):
Dim Cn as New ADODB. Connection
TimeOut? setting is related to the network environment
Cn.connectiontimeout = 100
DSN Connection
#Cn. Open pubs, SA
DSN-free connection to SQL Server
Cn.open Driver={sql Server}; Server=server1; Uid=sa; pwd=;D Atabase=pubs
Cn.close
The following is an example of using transactions:
Dim Cn as New ADODB. Connection
Dim rs as New ADODB. Recordset
Open connection
Cn.open pubs, SA
Open the Titles table
Rs. Open SELECT * FROM titles, Cn, adOpenDynamic, adlockpessimistic
Cn.begintrans
Make changes
Cn.committrans
or regret it:
cn. RollbackTrans
Cn.close
After establishing the connection, everything will be OK. The command object can be used to achieve full control of the database.