Use an include file for your database connection. Here's the reason
why. Before I wised up to this, I was placing the connection on every
page. Then there were some configuration changes and none of the pages
worked. This meant that every page had to be edited.
Now lets take a look at how to do this. The first thing to do is
create a directory in the ROOT of the web specifically for placing the
include file. One important thing to note is that include files are
commonly named with a .inc extension. This is a BAD thing to do. Use
the .asp extension as some unscrupulous person may try to guess the
file name. IF they get lucky they can call your .inc in the browser
and read it. Thus they have the path to the database.
Now down to the nuts and bolts. When we want to include some code
or html somewhere, all that's needed is the page we want to include
and this line here:
<!-- #Include Virtual="folder/file.asp" -->
This line tells the server that the FILE.ASP is in a directory
named FOLDER in the ROOT of the web. Just place it where you would
have normally placed your connection string. Using a VIRTUAL include
like this will allow you to place this on any page no matter where in
the web or how many directories deep it is since it is telling the
server to look in the root for the folder.
Now our FILE.ASP has our database connection in it and would actually
look like this:
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\InetPub\mdb\stuff.mdb;"
%>
Notice I am using Jet for the connection. This is the best method
for a DSN-Less connection. You could also use the Access driver as as
well like so.
Now if you ever need to make a change in your connection string all
you do is change the include file, in this case FILE.ASP. There is no
need to do anything to any other pages other than the actual included
file.