Pages

Tuesday 10 April 2018

Accessing ActiveX Data Objects (ADO) With JScript

The JScript implementation of ActiveX Data Objects is almost identical to that of VBScript. The only difference is in the syntax. JScript server-side scripts are used within Active Server Pages and (with the help of Internet Information Server) are issued to a client web browser.

Referencing ActiveX Data Objects:-
Once difference between the VBScript and JScript implementations of ADO is the name of the include file for ActiveX Data Objects. In JScript, the filename is adojavas.inc. To add it to an Active Server Page, type the following line:

<!--#include file="adojavas.inc"-->
Creating ActiveX Data Objects:-The first thing you need to do is create the variables that will hold your objects:

// define a variable which will be used as a reference to the
// Connection object
var con;

// define a variable which will be used as a reference to the
// Recordset object
var rec;


Once you have the variable references, you can create ActiveX Data Objects with the CreateObject function of the Server object just as in VBScript:

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

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


Again, always remove your unused objects by setting them to null:

' remove the objects
con = null;
rst = null;

Using ADO with JScript: An Example:-
The last example in this chapter is very similar to the VBScript example. The JScript program in Example 3-5 illustrates how an Active Server Page can use the JScript scripting language to create and instantiate ActiveX Data Objects on an Internet Information Server in order to create standard static HTML pages to be sent back to a requesting client. The ASP page should be stored in a file with an .asp extension that is located in an IIS virtual directory.

Once again, ensure that the Biblio.mdb file resides in the C:\Program Files\Microsoft Visual Studio\VB98 directory or that the directory entered in the ASP source matches the location of the Access database file.

Example 3-5. A Simple ASP Page Using JScript:-

<% @LANGUAGE="JScript" %>
<!--#include file="adojavas.inc"-->

<html>
<head>
<title>Example of ADO using JScript</title>
</head>
<body>
<script LANGUAGE="JScript" RUNAT="server">
// define our variables which will be used as references to our
// ActiveX Data Objects and instantiate a new Connection object
var con = Server.CreateObject("ADODB.Connection");
var rst;
Response.write("Connection object created.<BR>");
// create two strings for use with the creation of a connection
// and a recordset
var sConString;
var sSQLString;
// create temporary variable for Execute method call
var lRecordsAffected;
// 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, "", "", -1);
Response.write("Connection opened.<BR>");
// create a Recordset object from a SQL string
sSQLString = "SELECT TOP 10 Author FROM Authors";
rst = con.Execute(sSQLString,
lRecordsAffected,
adCmdText);
Response.write("SQL statement processed.<BR>");
// retrieve all the data within the Recordset object
Response.write("Getting data now...<BR><BR>");
while (!rst.EOF) {
Response.write(rst("Author") + "<BR>");
rst.MoveNext( );
}
Response.write("<BR>End of data.<BR>");
// close and remove the Recordset object from memory
rst.Close( );
rst = null;
Response.write("Closed and removed " +
"Recordset object from memory.<BR>");
// close and remove the Connection object from memory
con.Close( );
con = null;
Response.write("Closed and removed " +
"Connection object from memory.<BR>");
</script>
</body>
</html>

No comments:

Post a Comment