Pages

Tuesday 10 April 2018

Accessing ActiveX Data Objects (ADO) With VBScript

ActiveX Data Objects can be accessed from within server-side scripts via Active Server Pages, better known as ASP (which in this case does not stand for Application Service Provider). Although this book does not go into ASP in detail,[For more detailed information, see ASP in a Nutshell, Second Edition by A. Keyton Weissinger (O'Reilly & Associates, 2000), which goes into depth about how to incorporate ADO into your ASP pages. In addition, Developing ASP Components by Shelley Powers (O'Reilly & Associates, 1999) covers accessing ADO from Visual Basic and Visual J++, discussing how to create an OLE DB simple data provider.] a brief explanation of the technology is needed to understand how to develop VBScript code that uses ActiveX Data Objects.

When a client requests an ASP (Active Server Page) from a server, the ASP is "executed" before it is sent to the calling client. If there are any scripts embedded within the Active Server Page, they are executed. The result of this execution of different scripts is a static HTML page that can be viewed by virtually any web browser.

Active X Data Objects therefore can be embedded within a server-side script in order to gather and display information for the client in a low-resource-intensive manner. Because the ADO code is run on a server, the HTML page contains only the result, not the code. Once the page has been dynamically created by the server, it is passed back to the client for static reading. Because the web server does not pass actual recordsets, or rows of data, the potential savings in bandwidth can be considerable.

Referencing ActiveX Data Objects:-
In order to use ActiveX Data Objects from within your server-side scripts, your server must be running IIS (Internet Information Server) Version 3.0 or better. Along with IIS, you must of course have installed ADO, which is part of the MDAC installation. MDAC and IIS are included as part of the Windows 2000 operating system. Also, in order to use ADO constants, you should copy the file adovbs.inc to the directory in which your HTML pages that use ADO reside. You can reference the adovbs.inc file by adding the following line of code to your HTML source:
<!--#include file="adovbs.inc"-->
Creating ActiveX Data Objects:-
In VBScript, the Variant is the only datatype. This type can represent just about any type of information that you could possibly want it to. Although in Visual Basic developers usually try to avoid using the Variant datatype at all costs, it is a necessary component of almost any VBScript code.

The first step in creating our ActiveX Data Objects in VBScript, as in Visual Basic, is to define the variables that will be used as references to our ActiveX Data Objects:

' define our variables which will be used as references to the
' Connection and Recordset objects
Dim con
Dim rst


You should notice that I did not use the As datatype notation in the variable-declaration statements. This is because VBScript does not allow us to define variables as a particular type. Because of this, we cannot directly create our variables as ADO objects. Instead, we must use late binding through the CreateObject method of the Server object to assign ActiveX Data objects to our Variant variables:

' create a new instance of an ADO Connection object
Set con = Server.CreateObject("ADODB.Connection")

' create a new instance of an ADO Recordset object
Set rst = Server.CreateObject("ADODB.Recordset")


Just as in Visual Basic, it is always good practice to remove your objects from memory before your code ends:

' remove the objects
Set con = Nothing
Set rst = Nothing

Using ADO with VBScript: An Example:-
Example 3-4 uses VBScript along with ActiveX Data Objects to create a static HTML sheet that can be passed from the Microsoft Internet Information Server to a client's web browser. It must be assigned a filename ending with an .asp extension and it must be stored in an IIS virtual directory so that IIS recognizes it as an Active Server Page. As with the other projects, ensure that the Biblio.mdb file is located in the C:\Program Files\Microsoft Visual Studio\VB98 directory or that the correct location is entered in the ASP page that you create.

Example 3-4. A Simple ASP Example Using VBScript:-

<% @LANGUAGE="VBScript" %>
<% Option Explicit %>
<!--#include file="adovbs.inc"-->
<html>
<head>
<title>Example of ADO using VBScript</title>
</head>
<body>
<%
' define our variables which will be used as references to our
' ActiveX Data Objects
Dim con
Dim rst
' create two strings for use with the creation of a connection
' and a recordset
Dim sConString
Dim sSQLString
' create a new instance of an ADO Connection object
Set con = Server.CreateObject("ADODB.Connection")
Response.Write "Connection object created.<BR>"
' open the BiblioDSN data source with the Connection object
sConString = "Provider=Microsoft.Jet.OLEDB.4.0; " _
& "Data Source=C:\\Program Files\\" _
& "Microsoft Visual Studio\\" _
& "VB98\\Biblio.mdb"
con.Open sConString
Response.Write "Connection opened.<BR>"
' create a Recordset object from a SQL string
sSQLString = "SELECT TOP 10 Author FROM Authors"
Set rst = con.Execute(sSQLString)
Response.Write "SQL statement processed.<BR>"
' retrieve all the data within the Recordset object
Response.Write "Getting data now...<BR><BR>"
Do Until (rst.EOF)
Response.Write rst("Author") & "<BR>"
rst.MoveNext
Loop
Response.Write "<BR>End of data.<BR>"
' close and remove the Recordset object from memory
rst.Close
Set rst = Nothing
Response.Write "Closed and removed " _
& "Recordset object from memory.<BR>"
' close and remove the Connection object from memory
con.Close
Set con = Nothing
Response.Write "Closed and removed " _
& "Connection object from memory.<BR>"
%>
</body>
</html>
As with the other examples shown so far, the previous code may not mean too much to you yet. Right now, remember that when implementing ADO with VBScript, there are two important things that you should always remember. The first is that all variables are created as Variant datatypes. The second is that you must use late binding through the use of the Server. Create Object method in order to assign a new instance of an ActiveX Data Object to a Variant datatype.

No comments:

Post a Comment