Using Server.MapPath in database connection strings

marlinlineInternet and Web Development

Oct 31, 2013 (3 years and 10 months ago)

88 views

Using Server.MapPath in database connection strings

The purpose of the
MapPath

method of ASP
Server

object is to translate the logical
path information that might be used by a client browser, into a physical path on the
server. Users often take advantage
of this ASP feature when using OLEDB, and
ODBC DSNless connection strings.

If you have a file based database remotely mounted on your server, you must know
the full path to that database in order to use a literal database connection string.
However, there
will be times you want to access such a database, but won't be able to
count on the file's physical path. To find a file's physical path, you can use the
Server.MapPath

method. Its only parameter is the file's virtual, or relative, path.

If the parameter y
ou pass starts with either a forward slash or a backslash,
Server.MapPath

assumes you want the full physical path that maps to the full virtual
path. If the path parameter doesn't start with any slash, then
Server.MapPath

assumes you want the physical path

in relation to the location of the calling ASP file.
For database connection string purposes, the full physical path is required.

Macromedia Dreamweaver offers the capability to use
Server.MapPath

in database
connection strings. The following examples ill
ustrate acceptable syntax in such
strings.



DSNless (ADO) Connection Strings


o

Standard Connection String:


"DRIVER={Microsoft Access Driver
(*.mdb)};DBQ=c:
\
inetpub
\
wwwroot
\
dw
\
asp
\
dw.mdb"


o

MapPath Connection String:


"DRIVER={Microsoft Access Driver (*.mdb)}
;DBQ=" &
Server.MapPath("/dw/asp/dw.mdb") & ";"




OLEDB Connection Strings


o

Standard Connection String:


"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:
\
inetpub
\
wwwroot
\
dw
\
asp
\
dw.mdb"


o

MapPath Connection String:


"Provider=Microsoft.Jet.OLEDB.4.0;Data Sour
ce=" &
Server.MapPath("/dw/asp/dw.mdb")


Using Driver on Application Server versus Local Machine (Windows)

When defining a Custom Connection String in Dreamweaver, you have the option of
using a driver on the Testing Server or on the Local Machine.

Note:

T
his option is not available in the Macintosh version of Dreamweaver. All
connections use the Application Server.

In the Windows version, you
must

select
Using Driver on Testing Server

when
utilizing the
Server.MapPath

approach.

When using the Server ASP ob
ject, it can only be invoked via a web and application
server. This is the connection method used when Using Driver on Testing Server is
selected. Selecting Using Driver on Local Machine connects to the database directly
via ODBC; the web server, and thus
the ASP Application Server, are not invoked.

It is acceptable to select Using Driver on Testing Server when the server resides on
the same machine as Dreamweaver, as when using Personal Web Server.

The following image shows the dialog box used to define a
Custom Connection String
in Dreamweaver using
Server.MapPath

and one of the connection string examples
displayed above.