Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
93 views

Accessing Databases Using IIS Web Server and ASP Object

The document discusses connecting to databases from ASP web pages using various connection strings. It provides requirements and sample connection strings for connecting to Microsoft Access, SQL Server, Visual FoxPro, and Oracle databases. It also describes several built-in ASP objects like Application, Request, Response, Session and Server that can be used to store and retrieve information and perform other tasks when building ASP applications and web pages.

Uploaded by

vivekkumars
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Accessing Databases Using IIS Web Server and ASP Object

The document discusses connecting to databases from ASP web pages using various connection strings. It provides requirements and sample connection strings for connecting to Microsoft Access, SQL Server, Visual FoxPro, and Oracle databases. It also describes several built-in ASP objects like Application, Request, Response, Session and Server that can be used to store and retrieve information and perform other tasks when building ASP applications and web pages.

Uploaded by

vivekkumars
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

DICS504

Internet and Web Designing

Accessing databases using IIS web server

There are many ways to connect to a database. This step-by-step article provides sample
connection strings for various types of databases and database connections.
Requirements

The following are the requirements for connecting to a database:

 Active Server Pages (ASP) enabled Internet Information Services (IIS) version 5.0 Web
server with Microsoft Data Access Components (MDAC) version 2.5 or 2.6 (with a Jet
database engine)
 Connectivity to a local or remote database
 ASP enabled Microsoft Internet Explorer version 5.0 or later

Sample Database Connection Strings

These examples are for demonstration purposes only. You must paste this code in your ASP code
to make a connection to the specified database. Note that you must change elements such as
database name, server name, database location, Data Source Name (DSN), and so on.

Microsoft Access
Without DSN
<%
Set Cnn = Server.CreateObject("ADODB.Connection")
Cnn.open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\mydatabase.mdb"
%>
OLE DB
<%
Set Cnn = Server.CreateObject("ADODB.Connection")
Cnn.open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=c:\mydatabase.mdb"
%>
File DSN
<% Set Cnn = Server.CreateObject("ADODB.Connection")
Cnn.open "FILEDSN=ADSN"
%>
With DSN and no User ID/Password
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.open "DSNname"
%>
With DSN and User ID/Password
<%
Set Conn = Server.CreateObject("ADODB.Connection")

229
Conn.open "DSNname","username","password"
%>
Without DSN, using a physical path as a reference
<%
Set Conn = Server.CreateObject("ADODB.Connection")
DSNtest="DRIVER={Microsoft Access Driver (*.mdb)}; "
DSNtest=dsntest & "DBQ=c:\mydatabase.mdb"
Conn.Open DSNtest
%>
Without DSN, using Server.MapPath

NOTE: Server.MapPath is the path from the Web server root. By default, this is
C:\Inetpub\Wwwroot.
<%
Set Conn = Server.CreateObject("ADODB.Connection")
DSNtest="DRIVER={Microsoft Access Driver (*.mdb)}; "
DSNtest=dsntest & "DBQ=" & Server.MapPath("/databases/mydatabase.mdb")
Conn.Open DSNtest
%>

Microsoft SQL Server

OLE DB
<%
Set cnn = Server.CreateObject("ADODB.Connection")
cnn.open "PROVIDER=SQLOLEDB;DATA
SOURCE=sqlservername;UID=username;PWD=password;DATABASE=mydatabase "
%>
With DSN
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.open "DSN=MyDSN;UID=user;PWD=password;DATABASE=mydatabase"
%>
Without DSN
<%
Set Conn = Server.CreateObject("ADODB.Connection")
DSNtest="DRIVER={SQL
Server};SERVER=ServerName;UID=USER;PWD=password;DATABASE=mydatabase"
Conn.open DSNtest
%>

Microsoft Visual FoxPro

Without DSN
<%
Set Conn = Server.CreateObject("ADODB.Connection")
ConnStr= "Driver=Microsoft Visual Foxpro Driver;
UID=userID;SourceType=DBC;SourceDB=C:\databases\mydatabase.dbc"
Conn.Open ConnStr
%>

230
Oracle

ODBC with DSN


<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.cursorlocation=adUseClient
' requires use of adovbs.inc; numeric value is 3
Conn.open "DSN=test;UID=name;PWD=pass"
%>
OLE DB
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.cursorlocation=adUseClient
' requires use of adovbs.inc; numeric value is 3
DSNTest="Provider=MSDAORA.1;Password=pass;User ID=name;Data Source=data.world"
Conn.open DSNtest
%>

ASP objects
ASP enabled Web server provides a number of built-in run-time objects as part of the server
side scripting environment:
 Request: An object holding information the client sends to the server as an HTTP
request.
 Response: An object holding information the server sends to the client as an HTTP
response.
 Server: An object holding information common to running virtual directories.
 Application: An object holding information common to all running HTTP sessions
under a single virtual Web directory.
 Session: An object representing a sequence of HTTP requests and responses from one
client system.
The following diagram illustrates the relations among the ASP objects:

231
ASP provided six built-in objects:
• Application
• Request
• Response
• Session
• Server
• ASPError

Application Object:

This Object is used to store information that can be shared among all the users of an
application.

Method
Because the Application object can be shared by more than one user, there are Lock and
Unlock methods to ensure that multiple users do not try to alter a property simultaneously.
Lock: Locks the application object so that only one user at a time can modify the values.
Unlock: Unlocks the application object allowing other users to modify application level
variables.
The following example uses the application variable NumVisits to store the number of times
that a particular page has been accessed. The Lock method is called to ensure that only the
current client can access or alter NumVisits. Calling the Unlock method then enables other
users to access the Application object.
<%
Application.Lock Application("NumVisits") = Application("NumVisits") + 1
Application.Unlock
%>
This application page has been visited
<%= Application("NumVisits") %> times!

232
Events
Application object Support two events
• Application_OnEnd
• Application_OnStart

Application_OnEnd
This event occurs when the IIS is shut down after Session_OnEnd event. All the variables are
destroyed after that.
Application_OnStart
This event occurs when the first .asp page is called after starting the IIS. Application level
variables can be declared here.

Request Object

Request object is used to retrieve all the information sent in a request from client browser to
web web server 
 Request Object Support following Collections
 Cookies
 Form
 QueryString
 ServerVariables
 ClientCertificate   

Cookies 
The Cookies collection enables you to retrieve the values of the cookies sent in an HTTP
request.Cookies should never be used to store secure data, such as passwords. Cookies are
transmitted as clear text. If a malicious user taps an Internet connection, then they can take
cookie data to impersonate a client and gain access to their data. 

Form
The Form collection retrieves the values of form elements posted to the HTTP request body,
with a form using the POST method.

QueryString
The QueryString collection retrieves the values of the variables in the HTTP query string.
The HTTP query string is specified by the values following the question mark (?). Several
different processes can generate a query string.

Query strings are also generated by sending a form or by a user typing a query into the
address box of the browser

All variables can be accessed directly by calling Request(variable) without the collection


name. In this case, the Web server searches the collections in the following order:Compose
 QueryString
 Form
 Cookies
 ClientCertificate
233
 ServerVariables

If a variable with the same name exists in more than one collection, the Request object
returns the first instance that the object encounters.
It is strongly recommended that when referring to members of a collection the full name be
used. For example, rather than Request.("AUTH_USER")
use Request.ServerVariables("AUTH_USER"). This allows the server to locate the item
more quickly.

ServerVariables
The ServerVariables collection retrieves the values of predetermined environment variables
Response Object 

We can use the Response object to send output back to the client from the Web Server.
Methods
The Response object   defines different  methods but most of the commonly used methods are
as follows .
BinaryWrite : Writes the given information to the current HTTP output without any
character-set conversion.
Redirect :Sends a redirect message to the browser, causing it to attempt to connect to a
different URL.
Write:Writes a variable or text to the current HTTP output as a string.
End : Stops processing the .asp file and returns the current result.

Properties
Buffer :Indicates whether page output is buffered.
ContentType :Specifies the HTTP content type for the response.

Session Object

You can use the Session object to store information needed for a particular user session.
Variables stored in the Session object are not discarded when the user jumps between pages
in the application; instead, these variables persist for the entire user session.
The server destroys the Session object when the session expires or is abandoned.
Methods
Abandon :This method destroys a Session object and releases its resources.
Contents.RemoveAll :This method deletes an item from the Contents collection.
Contents.Remove :This method deletes all items from the Contents collection.

Properties
The Session object defines the following properties.
SessionID :Returns the session identification for this user.
Timeout: The time-out period for the session state for this application, in minutes

Events
Session_OnEnd Event 
Session_OnStart Event
Server Object

234
We can use the Server object to access various utility functions of server.
Methods
CreateObject :The CreateObject method creates an instance of a server component.
GetLastError : The GetLastError method returns an ASPError Object describing the error
condition that occurred. This method is available only before the .asp file has sent any
content to the client.

Example Code
The following three examples demonstrate different errors that generate a 500;100 custom
error. The three types of errors are:
• Preprocessing errors
• Script compiling errors
• Run-time errors
The following example demonstrates a preprocessing error, which IIS generates when it tries
to include the file. This error will be generated because the #include statement is missing the
file parameter for the #include statement.
<% response.write "hello" %>

The following example demonstrates a script compiling error. The scripting engine does not
compile this script because it is missing the keyword next in a For...Next loop.

<% dim I for i=1 to 1 nxt %>


The following example demonstrates a run-time error that occurs because the script attempts
to divide by 0.
<% dim i,j dim sum sum=0 j=0 for i=1 to 10 sum=sum+1 next sum=sum/j %>

If a 500;100 custom error has been defined for an ASP application, it may refer to an .asp
file. In this case, when an error occurs during the running of an .asp file within the
application, the server automatically transfers to this ASP page via the Server.Transfer
method. All of the state information from the executing ASP application will be available to
the .asp file that is handling the error. In addition, the ASPError Object will be available, so
you can expose the properties of the error through the .asp file that you set up to handle the
error.

The default Web site is configured to use the file \iishelp\common\500-100.asp. You can
either use this file for processing ASP errors, or create your own. If you want to change the
.asp file for processing the 500;100 custom errors you can use IIS Manager.
Note:
A 500;100 custom error will be generated if IIS encounters an error while processing either
an .asp file or the application's Global.asa file.

HTMLEncode : Applies HTML encoding to the specified string.

MapPath : Maps the specified virtual path, either the absolute path on the current server or
the path relative to the current page, into a physical path.

Transfer : Sends all of the current state information to another .asp file for processing.

235
Properties
ScriptTimeout :The amount of time that a script can run before it times out.

ASPError Object

You can use the ASPError object to obtain information about an error condition that has
occurred in script in an ASP page. The ASPError object is returned by
the Server.GetLastError method. The ASPError object exposes read-only properties.
You can use the ASPError object in an ASP page that is configured to respond to an HTTP
error. For more information about configuring custom errors, see Creating Custom Error
Messages.
Methods
The ASPError object has no methods.
Properties
The ASPError object defines the following properties.

Property Description

ASPError.ASPCode Returns an error code generated by IIS.

Returns a more detailed description of the error, if it is an ASP-


ASPError.ASPDescription
related error

Indicates if the source of the error was internal to ASP, the


ASPError.Category
scripting language, or an object.

Indicates the column position within the .asp file that generated the
ASPError.Column
error.

ASPError.Description Returns a short description of the error.

Indicates the name of the .asp file that was being processed when
ASPError.File
the error occurred.

ASPError.Line Indicates the line within the .asp file that generated the error.

ASPError.Number Returns the standard COM error code.

Returns the actual source code, when available, of the line that
ASPError.Source
caused the error.

236

You might also like