What is ADO? - Language Differences
(Page 11 of 12 )
I’ve tried to make this book relatively language-independent. Because ADO can be used by any programming language that supports COM, deciding which language to use for the samples in this book becomes sticky. To help the widest possible audience, I’ve stuck with a pseudo-code type style. For example, in the examples that show something being printed, I’ve just used Print. You can then substitute the command for your language of choice. The different methods for several common languages and environments are described here:
Language | Method | Notes |
Visual Basic | Debug.Print "message" | Prints the text to the debug window |
| MsgBox "message" | Pops up a window with the text |
ASP & VBScript | Response.Write "message" | Returns the text to the browser |
ASP & JavaScript | Response.Write ( "message"); | Returns the text to the browser |
VBScript | document.write "message" | Inserts message into the HTML document |
| MsgBox "message" | Pops up a window with the text |
JavaScript | document.write ( "message"); | Inserts message into the HTML document |
| alert ( "message"); | Pops up a window with the text |
Although I’ve tried to make our samples as language-independent as possible, you’ll notice that most of the samples in the book use a Visual Basic/VBScript style—because I see this as the greatest market for ADO usage. However, the samples should be easy to translate into your favorite language.
If you want to use ADO in a variety of languages, have a look at the document entitled Implementing ADO with Various Development Languages at http://msdn.microsoft.com/library/techart/msdn_adorosest.htm. (Editor's note: Link not currently available.)
A set of samples in a variety of languages is also available from the support page on the Apress Web site at http://support.apress.com/.
Creating Objects Creating the ADO objects is one area where pseudo-code doesn’t really work too well. Significant differences exist, even between such apparently similar languages as Visual Basic and VBScript. Therefore, this section is devoted to the act of creating objects. I describe in some detail how it’s done in each of the five languages where I think ADO will have most impact.
This isn’t intended as a full explanation of all the objects and how they are used in each language; instead, it is a demonstration of the major differences between several of the most common languages.
Visual Basic Before you can create an ADO object in Visual Basic, make sure you have a reference to the ActiveX Data Objects Database (ADODB) library set. From the Project menu, select References, and then choose Microsoft ActiveX Data Objects 2.8 Library. The ADO Extensions (ADOX) are in the library labeled Microsoft ADO Ext. 2.8 for DDL and Security. You can create objects three ways. The first is:
Dim objRs As New ADODB.Recordset
This creates the object reference immediately, but the object is not instantiated until the first property or method is called. This means that instead of being instantiated when declared, the object is instantiated later in the code—which can lead to debugging problems. A better solution is to use this method:
Dim objRs As ADODB.Recordset
Set objRs = New ADODB.Recordset
This creates a variable of the type Recordset, and then the Set line instantiates the object. The third method is the older style, using late binding, and is less used these days (and also doesn’t require a reference to the ADODB library to be set):
Dim objRs As Object
Set objRs = CreateObject("ADODB.Recordset")
After an object has been created, invoking the methods and properties is extremely simple. For example:
objRs.Cursorlocation = adUseClient
objRs.Open "authors", objConn, adOpenStatic, _
adLockBatchOptimistic, adCmdTable
The ad constants are automatically available to you in Visual Basic once you have referenced the ADO library (as described previously).
Looping through a recordset is just a question of using the MoveNext method and checking the EOF property:
While Not objRs.EOF
Debug.Print objRs(" field _ name")
objRs.MoveNext
Wend
ASP/VBScript Creating objects in VBScript is different from Visual Basic because VBScript doesn’t have variable types (all variables are of Variant type) or support for adding references to type libraries (although it does use the Visual Basic syntax for assigning object variables using Set). Therefore, there’s no need to define the variables, although it’s better to define them for ease of code maintenance and reliability:
Dim objRs
Set objRs = Server.CreateObject("ADODB.Recordset")
This creates a Recordset object in ASP script code.
You can also use the <% Option Explicit %> command to ensure that variables are required to be defined.
Using the object follows the same procedure as for Visual Basic:
objRs.CursorLocation = adUseClient
objRs.Open "authors", objConn, adOpenStatus, _
adLockBatchOptimistic, adCmdTable
The only difference here is that the constants are not automatically available; this is because scripting languages do not have access to the type library and its constants. You have two options. The first is to use the integer values that these constants represent. The problem with this is that your code becomes sprinkled with various numbers whose meaning is not obvious to the reader. For example, without ADO’s predefined constants, the previous statement would read:
objRs.CursorLocation = 3
objRs.Open "authors", objConn, 1, 3, 2
This makes your code harder to read, and therefore harder to maintain.
The second option is to include the constants in your ASP script; this means that you can use the constant names instead of their values. You can include the ADO constants with the following line:
<!— #INCLUDE FILE="adovbs.inc"—>
The include file, adovbs.inc, is installed in the default directory of Program Files\Common Files\System\ADO. It can be moved to your local ASP directory or referenced from a central virtual directory.
A better way to use the constants is to create a direct reference to the type library, using some meta data:
<!— METADATA TYPE="typelib"
FILE="C:\Program Files\Common Files\System\ADO _
\msado15.dll" —>
The advantage of this method is that you use the values from the ADO library itself rather than those from the include file. This means that you don’t have to worry that the location (or even the contents) of the include file might change. Note that the name of the DLL is always msado15.dll regardless of which ADO version you have.
Looping through recordsets in VBScript is exactly the same as in Visual Basic:
While Not objRs.EOF
Response.Write objRs.Fields(" field _ name").Value
objRs.MoveNext
Wend
This is from ADO Programmer's Reference, by Dave Sussman (Apress, ISBN 1590593421). Check it out at your favorite bookstore today. Buy this book now. |
Next: Creating Objects in JScript, Visual C and .NET >>
More ASP.NET Articles
More By Apress Publishing