The ADO Set of Records |
|
Creating a table provides you with a structure in which you can create a list of values. When a table has been created, it is initially empty but an empty placeholder is automatically created for each column:
Therefore, after creating a table, you can populate it. Since an empty placeholder is created under each column, you can start by adding one value for each category. When a value has been created for each column, even if you skip some columns, you are said to have created a record. Here is an example:
In the same way, you can keep adding records as you see fit. Every time you have created a record, a placeholder for a new record is automatically made available so you don't have to wonder when a record would go. This also implies that each new record you create is appended, that is, it is added to the end, of the table. Here is an example of a table with records:
|
The Recordset Object |
The group of records that belong to a table is called a set of records. In ADO, whenever you need to deal with the set of records of a table, you should first declare a variable that would hold the records. To do this, you must obtain an appropriate reference to ADODB. A set of records of a table in ADO is managed by an object called Recordset. After obtaining a reference to a Recordset object, in order to use it, you must open it. This is done using its Open() method whose syntax is: recordset.Open Source, ActiveConnection, CursorType, LockType, Options The first argument to this method can be a different thing depending on the circumstance. For example, if you are trying to deal with the records of a table, this argument can be the name of that table. The second argument specifies how the connection will be carried. It can be the name of a Connection reference you previously established. The rest of the arguments are optional. In other words, if you don't specify their values, they would use a default. The third argument specifies the type of cursor that would be used. The available options are stored in an enumerator called CursorTypeEnum and they are: |
Constant | Value | Description |
adOpenUnspecified | -1 | Does not specify the type of cursor |
adOpenForwardOnly | 0 | Uses a forward-only cursor Identical to a static cursor, except that you can only scroll forward through records. This improves performance when you need to make only one pass through a Recordset |
adOpenKeyset | 1 | Uses a keyset cursor Like a dynamic cursor, except that you can't see records that other users add, although records that other users delete are inaccessible from your Recordset. Data changes by other users are still visible |
adOpenDynamic | 2 | Uses a dynamic cursor Additions, changes, and deletions by other users are visible, and all types of movement through the Recordset are allowed, except for bookmarks, if the provider doesn't support them |
adOpenStatic | 3 | Uses a static cursor, which is a static copy of a set of records that you can use to find data or generate reports. Additions, changes, or deletions by other users are not visible |
The default value of this argument is adOpenForwardOnly. The fourth argument specifies the type of locking system that would be used on the set of records. The available options are stored in an enumerator called LockTypeEnum and they are: |
Constant | Value | Description |
adLockUnspecified | -1 | Does not specify a type of lock. For clones, the clone is created with the same lock type as the original |
adLockReadOnly | 1 | Indicates read-only records. You cannot alter the data |
adLockPessimistic | 2 | Indicates pessimistic locking, record by record. The provider does what is necessary to ensure successful editing of the records, usually by locking records at the data source immediately after editing |
adLockOptimistic | 3 | Indicates optimistic locking, record by record. The provider uses optimistic locking, locking records only when you call the Update method |
adLockBatchOptimistic | 4 | Indicates optimistic batch updates. Required for batch update mode |
The last argument is an option you specify about the first argument. |
Record Creation |
One of the actions you can perform using a Recordset object is to perform data entry, that is, to create new records. To support data entry, the Recordset object is equipped with the AddNew() method. Its syntax is: recordset.AddNew FieldList, Values The first argument is a name of a column and must be identified as belonging to an array called Fields. After specifying the name of the column in the parentheses of the Fields array, you can assign it the desired value. You can do this for each column or for selected columns of the table. After creating a record, you should remember to save it. This is done by calling the Update() method of the Recordset object.
|
Practical Learning: Performing Data Entry |
|
Record Insertion |
Besides, or as opposed to, adding a new record, if you don't want to formally use the Recordset class to create a record, you can use the Execute() method of the Connection object. To create a new record using the Connection object, you can call the Execute() method and pass it a string that contains the following formula: INSERT TableName VALUES(Name1, Name2, Name_n) or INSERT INTO TableName VALUES(Name1, Name2, Name_n)
|
Practical Learning: Inserting a new Record |
|
|
||
Previous | Copyright © 2001-2005 FunctionX, Inc. | Next |
|