Data | database one, ADO overview
ADO (ActiveX Data Object) is an optimized set of dedicated objects to access the database, providing a complete site database access solution for ASPs. The ADO object executes on the server side, and provides the content that contains the database information to the client, the client can also read and write to the database according to the specified permission return parameter. ADO is characterized by fast execution, simple use, low memory consumption and small hard disk space.
Because ADO accesses a database through ODBC, it can connect to a variety of ODBC-enabled databases, such as Access, SQL Server, Oracle, Informix, and so on. Before using ADO, add the appropriate database driver in ODBC and create the appropriate DSN (data source name). ADO contains many objects, where connection objects and Recordset objects are used primarily to control database access, and to establish a database access first you create a connection object. It then completes the operation of the database with the Recordset object based on the connection object.
Second, the Connection object
The Connection object represents an open connection to an OLE DB data source, a bit like a real network connection to the server in a Client/server database application, and we can create a connection object independently of any other object. Note that you first create the connection object before referencing a connection, which has the following syntax, where connection is the variable that references the Connection object: Setconnection=server.creatobject ("ADODB.") Connetcion ")
The connection object contains more than one property, and the following are several commonly used properties, noting that you can change its properties only after the connection object is created and before the object is opened. Syntax such as: Connection. Attribute = property value
1. ConnectionString a string containing the connection information, passing a "DSN name" or a string consisting of "argument = value" to the ConnectionString property. You can specify a data source for the connection object, which can be set before the connection is opened or set in the Open command. The argument in ADO are as follows:
Parameter description
Provider the creator of the connection object, the default value is Msdasql (Microsoft ODBC Provider for OLE DB)
Data source Specifies the connection ODBC datasource (DSN)
User setting the username to open the connection
Password set the password required to open the connection
File Name files that contain the connection information specified by the creator
2. Mode
You can set the parameters for the following constants to set the user's access rights to the current connection.
Constant value meaning
adModeUnknown 0 does not set operation permissions on the database (default)
adModeRead 1 Read-only
adModeWrite 2 Write only
adModeReadWrite 3 can read and write
adModeShareDenyRead 4 Disables the creation of additional read-only connections to the data source
adModeShareDenyWrite 8 Disables the creation of additional write-only connections to the data source
Admodeshareexclusive 12 disables the creation of additional read/write connections to the data source
adModeShareDenyNone 16 prevents any other connections to the data source from being established
3. ConnectionTimeout specifies the time to wait for the connection to be established and, if timed out, interrupts the request and gives an error message. Wait time in seconds, you can assign an integer value to this parameter, the default value is 15 seconds. Setting the ConnectionTimeout property can give you a hint when the network is congested or when the server is busy, preventing endless waiting. If the value of the ConnectionTimeout is set to 0, the system waits until the connection is established successfully.
4. CommandTimeout Specifies the wait time when executing the Execute command, and breaks the request and gives an error message if the timeout occurs. Wait time in seconds, you can assign an integer value to this parameter, the default value is 30 seconds. Use the open, close, and execute methods of the Connection object to complete all database access actions from establishing a database connection to manipulating the database and finally shutting down the connection.
. The open open method opens the physical connection between the connection object and the data source, as follows:
Connection. Open Connectionstring,userid, Password
Connection represents an object variable that references a connection object that already exists.
ConnectionString an optional variable, meaning the same as the ConnectionString attribute mentioned earlier.
UserID, an optional variable, is a string containing the user name used to establish the connection.
Password, an optional variable, is a string containing the password used to establish the connection.
. Close closes a connection object that has already been opened with the closed method, but at this point the connection object is not removed from memory and we can also change its property arguments or open the object. To really free up the memory space occupied by the object, you need to set this object to "nothing." If other Recordset objects are also opened based on this connection object, all related Recordset objects are closed while the connection object is closed.
. Execute uses execute to execute an SQL statement or a stored procedure based on a connection object that has already been opened, and to point to the resulting set of results with a Recordset object. The syntax for the Execute command is as follows:
Connection. Execute CommandText, RecordsAffected, Options
CommandText string that contains the SQL statement, table name, or stored procedure that will be executed.
RecordsAffected an optional parameter, a long integer variable, whose return value is the number of records affected by the manipulation after execution.
Options optional parameter that describes what manipulation parameters are included in the CommandText, with the following available values:
Constant value meaning
adCmdText 1 CommandText is a command described in SQL statements
adCmdTable 2 CommandText is a table name
adCmdStoredProc 4 CommandText is a stored procedure
adCmdUnknown 8 CommandText is an unknown command type
Various methods of using the connection object can complete various access actions to the database, such as adding, deleting, updating, and selecting.
[1] [2] Next page