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.
Nuts Own Code

Asp Directory

Nuts Web Search
Web Directory
Web Search

Nuts ASP Code
Functions
Scripts
Tutorials
Wizards
Fonts
Download Fonts
Download Dll Files
Dll Files
Relaxing Games
Online Games
Take Our Poll
Do you use .Net?
Yes
No
Not Yet
Dot What?
Really, Take It!
Get The Source
About Nuts4Asp
Privacy
Contact Us
Arts
Movies, Television, Music
Business
Jobs, Industries, Investing
Computers
Internet, Software, Hardware
Games
Video Games, RPGs, Gambling
Health
Fitness, Medicine, Alternative
Home
Family, Consumers, Cooking
Kids and Teens
Arts, School Time, Teen Life
News
Media, Newspapers, Weather
Reference
Maps, Education, Libraries
Regional
US, Canada, UK, Europe
Science
Biology, Psychology, Physics
Sports
Baseball, Soccer, Basketball
World
Deutsch, Español, Français, Italiano, Japanese, Nederlands, Polska, Svenska
Help build the largest human-edited directory of the web
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.

<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=
C:\InetPub\mdb\stuff.mdb;"
%>

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.

More ways to use include files.

Bad or missing query parameters in request.

Copyright © 2001-2008 Nuts4Asp.com
All other names used are property or copyright of their respective owners.