Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
Many enterprise scripts require you to perform an action on multiple records in the database. For example, you might have a script that updates the hardware inventory for a set of computers. The script needs to connect to the database, locate the record for the first computer, and apply any required updates. The script must then repeat the process for each computer being inventoried.
It is possible to create an SQL query that returns only the name of computer being updated (for example, "SELECT * FROM Inventory WHERE ComputerName = WebServer "). However, if your script must update 100 computers, this would require 100 separate SQL queries being passed to the database server, one for each computer being updated. A more efficient approach might be to download the entire set of computers and then use the Recordset object Find method to locate each computer as needed.
The Find method requires two steps:
strSearchCriteria =
"ComputerName = 'WebServer'
"
objRecordSet.Find strSearchCriteria
You can use the Recordset object EOF (end-of-file) property to determine whether a record was found. If EOF is True, this means the recordset has been searched from beginning to end and no record was found. This is important because your script will fail if it attempts to take action on a record that does not exist. For example, you might use the Find method to locate a particular record and then use the Delete method to delete that record. If the record does not exist, however, both the Delete method and your script will fail.
Using the Find method also helps you eliminate duplicate records within a database. For example, when creating an inventory of your computers, you probably want one record per computer; you do not want multiple records for the computer WebServer. To prevent the possibility of creating duplicate records, follow a procedure similar to this:
Listing 17.17 contains a script that finds a record in a recordset. To carry out this task, the script must perform the following steps:
These constants will be used to configure the CursorLocation, CursorType, and LockType for the connection.
The Connection object makes it possible for you to issue queries and other database commands.
The Recordset object stores the data returned from your query.
Be sure to append a semicolon (;) to the DSN name.
The Open method requires four parameters:
The variable strSearchCriteria will serve as the search criteria for the Find method.
Listing 17.17 Finding Records in a Recordset
|
|
Send us your feedback | « Previous | Next » |