Global.asa and DSN-less database connections

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default Global.asa and DSN-less database connections

    OBJECTIVE: I am trying to create a connection to an access database in and then use that connection ASP pages in my application without having to re-connect to the database.

    I have used the following code in the global.asa file in the Session_OnStart sub to create a connection (which works fine):
    strPath = "C:\Data\WebProjects\GuestBook\db.mdb"
    Set conSQLServer = Server.CreateObject("ADODB.Connection")
    conSQLServer.Open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & strPath

    However, when I use the following code in another ASP file, the get an "object required" error:
    Set rstData = conSQLServer.Execute ("<sql-statement>")
    ......which indicates that rstData is not available at application or even session level.

    Any help would be greatly appreciated.

    Many thanks,

    David.
    David Raskino Guest

  2. Similar Questions and Discussions

    1. increase database connections
      Hi all, Currently, I notice that there are only two database connections between my ColdFusion server and the SQL server. May I ask how can I...
    2. MX Open Database Connections
      Did MX do away with the settings for database connections? We just switched from Oracle 8i to Oracle 9i and have seen some connectivity failures. ...
    3. CD/DVD and Database connections
      I need to create a standalone "app" that allows the users to insert the CD/DVD and be able to query a database that is on the CD/DVD. I need to...
    4. Dreamweaver and database connections
      I've been hand coding my ASP database connections etc, but thought I'd take a look a Dreamweavers' (MX) ability to write the code - it seems to have...
    5. #20006 [Ana->Ver]: cannot use 2 database connections
      ID: 20006 Updated by: sniper@php.net Reported By: kezal at mail dot ru -Status: Analyzed +Status: ...
  3. #2

    Default Re: Global.asa and DSN-less database connections

    Code in global.asa only runs on the start of a session or application
    (depending on which sub you put it in) or at the end of the session or
    application. Create a subroutine to create your connection in a common
    include file and call it in the pages that need the connection. While it is
    possible to create a connection object and store it in a session or
    application variable, it is strongly discouraged. It is best to create an
    adodb.connection, connect, get your recordset, close, and destroy on each
    page individually. But, you can create a sub like so in an include file and
    include it in all your pages that need the connection.

    in include:

    Dim oADO
    Sub OpenData()
    Set oADO = CreateObject("ADODB.Connection")
    oADO.Open yourConnectionString
    End Sub

    Sub CloseData()
    oADO.Close
    Set oADO = Nothing
    End Sub


    in .asp file:
    Call OpenData()
    Set yourRS = oADO.Execute(YourQuery)
    '''code
    Call CloseData()


    You can also do things like Function GetRecordSet() in your include that
    would get a recordset, and return it as an array with .GetRows() and things
    along those lines. Whatever you do, do not try to create and open a
    connection and continuously leave it open.

    Ray at home


    "David Raskino" <DavidRaskino@woodstock.ac.in> wrote in message
    news:7E26701A-4D4E-4BC8-8E09-E8A5B90AF8B8@microsoft.com...
    > OBJECTIVE: I am trying to create a connection to an access database in and
    then use that connection ASP pages in my application without having to
    re-connect to the database.
    >
    > I have used the following code in the global.asa file in the
    Session_OnStart sub to create a connection (which works fine):
    > strPath = "C:\Data\WebProjects\GuestBook\db.mdb"
    > Set conSQLServer = Server.CreateObject("ADODB.Connection")
    > conSQLServer.Open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" &
    strPath
    >
    > However, when I use the following code in another ASP file, the get an
    "object required" error:
    > Set rstData = conSQLServer.Execute ("<sql-statement>")
    > ....which indicates that rstData is not available at application or even
    session level.
    >
    > Any help would be greatly appreciated.
    >
    > Many thanks,
    >
    > David.

    Ray at Guest

  4. #3

    Default Re: Global.asa and DSN-less database connections


    Here is my DBConn.asp code

    [url]http://www.coolpier.com/cp/cp_scripts/script.asp?file=DBConn.asp&view=code[/url]

    Brynn


    On Thu, 22 Jan 2004 00:01:14 -0500, "Ray at <%=sLocation%>"
    <myFirstNameATlane34dotKOMM> wrote:
    >Code in global.asa only runs on the start of a session or application
    >(depending on which sub you put it in) or at the end of the session or
    >application. Create a subroutine to create your connection in a common
    >include file and call it in the pages that need the connection. While it is
    >possible to create a connection object and store it in a session or
    >application variable, it is strongly discouraged. It is best to create an
    >adodb.connection, connect, get your recordset, close, and destroy on each
    >page individually. But, you can create a sub like so in an include file and
    >include it in all your pages that need the connection.
    >
    >in include:
    >
    >Dim oADO
    >Sub OpenData()
    > Set oADO = CreateObject("ADODB.Connection")
    > oADO.Open yourConnectionString
    >End Sub
    >
    >Sub CloseData()
    > oADO.Close
    > Set oADO = Nothing
    >End Sub
    >
    >
    >in .asp file:
    >Call OpenData()
    >Set yourRS = oADO.Execute(YourQuery)
    >'''code
    >Call CloseData()
    >
    >
    >You can also do things like Function GetRecordSet() in your include that
    >would get a recordset, and return it as an array with .GetRows() and things
    >along those lines. Whatever you do, do not try to create and open a
    >connection and continuously leave it open.
    >
    >Ray at home
    >
    >
    >"David Raskino" <DavidRaskino@woodstock.ac.in> wrote in message
    >news:7E26701A-4D4E-4BC8-8E09-E8A5B90AF8B8@microsoft.com...
    >> OBJECTIVE: I am trying to create a connection to an access database in and
    >then use that connection ASP pages in my application without having to
    >re-connect to the database.
    >>
    >> I have used the following code in the global.asa file in the
    >Session_OnStart sub to create a connection (which works fine):
    >> strPath = "C:\Data\WebProjects\GuestBook\db.mdb"
    >> Set conSQLServer = Server.CreateObject("ADODB.Connection")
    >> conSQLServer.Open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" &
    >strPath
    >>
    >> However, when I use the following code in another ASP file, the get an
    >"object required" error:
    >> Set rstData = conSQLServer.Execute ("<sql-statement>")
    >> ....which indicates that rstData is not available at application or even
    >session level.
    >>
    >> Any help would be greatly appreciated.
    >>
    >> Many thanks,
    >>
    >> David.
    >
    >
    Brynn
    [url]www.coolpier.com[/url]

    I participate in the group to help give examples of code.
    I do not guarantee the effects of any code posted.
    Test all code before use!
    Brynn Guest

  5. #4

    Default Re: Global.asa and DSN-less database connections

    "David Raskino" <DavidRaskino@woodstock.ac.in> wrote in message
    news:7E26701A-4D4E-4BC8-8E09-E8A5B90AF8B8@microsoft.com...
    > OBJECTIVE: I am trying to create a connection to an access database in and
    then use that connection ASP pages in my application without having to
    re-connect to the database.
    >
    Bad idea ...
    [url]http://www.aspfaq.com/show.asp?id=2053[/url]

    --
    Tom Kaminski IIS MVP
    [url]http://www.iistoolshed.com/[/url] - tools, scripts, and utilities for running IIS
    [url]http://mvp.support.microsoft.com/[/url]
    [url]http://www.microsoft.com/windowsserver2003/community/centers/iis/[/url]



    Tom Kaminski [MVP] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139