| << 13.1.2- Creating a Recordset | Chapter13 | 13.2.0- Characteristics of the Recordset Object >> |
Introducing the ADO Constants
When using ADO you'll find that there are predefined constants for a lot of the options. For example, in the Recordset.asp file above we used the following lines todefine three constants:
Const adOpenForwardOnly = 0
Const adLockReadOnly = 1
Const adCmdTable = 2
These constants were then used later in the code, to defineparticular features of the recordset:
objRS.Open "Movies", strConnect,adOpenForwardOnly, adLockReadOnly, adCmdTable
Here, we use these constants as arguments to the Open method – they tell the Openmethod more about the type of Recordset object wewant. The integer values of the constants are determined, and these values areinterpreted by ADO – which goes away and forms a particular type of recordset.In this case, they specify that we want a recordset with a forward-only,read-only cursor and that we're asking for an entire table (don't worry aboutwhat that means just now – we'll learn more about it later in thischapter).
So, the above Open command isinterpreted like this:
objRec.Open "Movies", strConnect, 0, 1, 2
Why Use ADO Constants?
In fact, we could have used either of the above versions ofthe Open statement in our code – the result wouldhave been the same. So which one should we choose?
Well, ADO doesn't mind which one you use. However, consider what happens when a colleague comes to read the code we've written. Yourcolleague would find the second version rather more difficult to understand –the integers 0, 1, 2 don't have any intuitive meaning, so it's rather lessobvious that we're trying to open a forward-only, read-only recordset with aquery in the form of a table.
In other words, we use constants in ADO because they makeour code easier to write and to read. That's why the ADO constants are givenspecial names – to reflect the purpose of the integer values that theyrepresent.
Using the ADO Type Library for Constants
So where did the names of these constants come from? Well,the authors of this book didn't invent them! There's a library of constantnames and values created by Microsoft, and available for us to use in our code.
If you ever write applications using languages such as Visual Basic or Visual C++, you'll find that these constants are automaticallyavailable to you once you reference the ADO typelibrary.
In order to save us from having to explicitly define theseconstants we can make use of the ADO type library in our ASP code. The ADO typelibrary is contained in a file called msado15.dll, which you should be able to find on yourweb server machine. The default location for this file is C:\Program Files\Common Files\System\ado. To use the ADO type library, you needto use a line such as the following:
<!-- METADATA TYPE="typelib"FILE="C:\Program Files\Common Files\System
\ado\msado15.dll"-->
You can include this METADATAstatement in each .asp file that uses ADOconstants where it's required (as we'll do in this book). Alternatively, youcan put it in the global.asa file, in which casethe constants will be available to every Web page in the application.
Using the msado15.dll typelibrary is the recommended way to make the ADO constants in your code. We'll bedemonstrating this in the remaining examples in this book.
adovbs.inc
If you'd like to see the values of the constants contained in the ADO type library, you can view them in the file adovbs.inc. This file is also contained in the defaultdirectory (probably C:\Program Files\Common Files\System\ado).If you want to view them, make a copy of the adovbs.incfile (into another directory) and open up the copy in Notepad. For example,along with the definition for adOpenForwardOnly,you'll find the constants for the other values we could use in its place:
'---- CursorTypeEnum Values ----
Const adOpenForwardOnly = 0
Const adOpenKeyset = 1
Const adOpenDynamic = 2
Const adOpenStatic = 3
Be careful not to change the contentsof adovbs.inc! After all, this file is a globalstore for ADO constants.
Although it's no longer recommended, it's possible to usethis file as a server-side include (SSI) file, to insert the ADO constantdefinitions into your .asp pages. To do this,you'd use a line such as this:
<!-- #INCLUDE FILE="C:\Program Files\CommonFiles\System\ado\adovbs.inc" -->
One disadvantage of this method is that it makes your ASPpage larger – because it defines nearly 400 constants, most of which youusually won't need. That doesn't happen when we use msado15.dll– because it's a dynamic link library (DLL), so the constants are only definedas they're needed.
To summarize this short section: the ADO constants are therefor use with all of the ADO objects, and make reading and writing ADO code mucheasier. In the remainder of the book, we'll use the ADO type library (msado15.dll) to define our ADO constants, via the METADATA statement shown above.
Now let's move back to the main subject of this chapter –the Recordset object.
| << 13.1.2- Creating a Recordset | Chapter13 | 13.2.0- Characteristics of the Recordset Object >> |

RSS
