• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

How do I store and read a Connection String in a config file?

I am trying to store a connection string in a file so I can call the same connection string from many asp pages.  Can anybody help  me with this?  I am new to ASP.
1 Solution
You'll want to store it in the global.asa page.  Here's a good tip on the subject:  http://www.devx.com/tips/Tip/13180
Here is how I do it:

1)  I create an include folder and put include files in it.  One such file I call cs.asp.  It may look something like:

  strConnect1 = "DSN=pervasiveDSN;ServerName=SERVER;UID=report;PWD=PW;ArrayBufferSize=8;TransportHint=TCP;ArrayFetchOn=1;AutoDoubleQuote=0;DecimalSymbol=."
  strConnect2 = "DSN=pervasiveDSN;ServerName=SERVER;UID=update;PWD=PW;ArrayBufferSize=8;TransportHint=TCP;ArrayFetchOn=1;AutoDoubleQuote=0;DecimalSymbol=."
  strConnect3 = "DSN=accessDSN;UID=IUSR_USER;PWD=PW;DBQ=\\server\share$\db.mdb;SystemDB=\\server\share$\db.mdw;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;"

2)  In my asp page at the top I'll code:

<!--  #include virtual="/include/cs.asp" -->

3)  Further in my asp page I'll make reference the connect string like:

  server.ScriptTimeout = 240
  set adoConn = server.CreateObject("ADODB.CONNECTION")
  adoConn.Open strConnect1
  Set oRS = server.CreateObject("ADODB.Recordset")

4)  Also, we lock down the include folder and only give the IUSR_<servername> read access to it...

Hope this helps,
By the way, the include folder should be off the root of the webserver folder:


Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

True, the include file is another method, but it does require you to add the include statement on every page.  The benefit of using the global.asa page and assigning an Application variable is that you control everything in one place.
Flower11Author Commented:
I am trying the Global.asa:


I created a Notepad file named Global.asa located on ftp://111.222.333.44/html/wwwroot/  The file contains:

<script language="vbscript" runat="server">
   Sub Application_OnStart
     Application("Conn") = "Provider=SQLOLEDB; Data Source=mydatasource; Initial Catalog=mycatalog; User ID=myUserID; Password=mypassword"
   End Sub

The ASP page tries to connect like this:

set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open Application("Conn")

What do I forget?  I'll try the include file tomorrow.

That looks right to me.  You may need to restart IIS.
in order to make global.asa to work

global.asa should be in a root directory,

you may create a virtual directory with a name MYSITE (like folder is C:\mywebsites\mysite)
and put your global.asa into mysite folder

Note: If your going to use global.asa - you might also need to set it as an application in the IIS manager in order for it to be executed. If it's not an application, IIS won't run it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now