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

Posted on 2006-05-23
Last Modified: 2008-02-26
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.
Question by:Flower11
    LVL 9

    Accepted Solution

    You'll want to store it in the global.asa page.  Here's a good tip on the subject:
    LVL 12

    Expert Comment

    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,
    LVL 12

    Expert Comment

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


    LVL 9

    Expert Comment

    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.
    LVL 1

    Author Comment

    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.

    LVL 9

    Expert Comment

    That looks right to me.  You may need to restart IIS.
    LVL 13

    Expert Comment

    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

    LVL 7

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now