Connect to Database Function

Discussion in 'ASP General' started by vunet, Aug 20, 2008.

  1. vunet

    vunet Guest

    Hello,
    I want to create a useful function or sub whichever works great for
    database connection to avoid typing same code all over. Earlier I was
    using include file for opening and closing adoCon object and
    connection. Now I really want to use a function to do it.
    Can one recommend a good example?

    I have this sample below which does not work for me because of the
    mappath being wrong depending on what file under what directory calls
    the file with this function:

    Sub DB_CONN
    Set adoCon = Server.CreateObject("ADODB.Connection")
    adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
    Server.MapPath("./db/web.mdb") & ";"
    End Sub

    Sub DB_CONN_CLOSE
    Set adoCon = nothing
    End Sub

    Thank you for any advice.
    vunet, Aug 20, 2008
    #1
    1. Advertising

  2. vunet wrote:
    > Hello,
    > I want to create a useful function or sub whichever works great for
    > database connection to avoid typing same code all over. Earlier I was
    > using include file for opening and closing adoCon object and
    > connection. Now I really want to use a function to do it.


    Where would you put that function? What is wrong with using an include
    file?

    > Can one recommend a good example?
    >
    > I have this sample below which does not work for me because of the
    > mappath being wrong depending on what file under what directory calls
    > the file with this function:
    >
    > Sub DB_CONN


    This isn't a function, it's a subroutine.

    > Set adoCon = Server.CreateObject("ADODB.Connection")
    > adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
    > Server.MapPath("./db/web.mdb") & ";"


    "/" gets to the root. I don't know what your directory structure is but
    something like this should work (you don't need the ending semicolon):

    adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & Server.MapPath("/db/web.mdb")


    > End Sub
    >
    > Sub DB_CONN_CLOSE
    > Set adoCon = nothing
    > End Sub
    >
    > Thank you for any advice.


    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Aug 20, 2008
    #2
    1. Advertising

  3. vunet

    vunet Guest

    On Aug 20, 12:34 pm, "Bob Barrows [MVP]" <>
    wrote:
    > vunet wrote:
    > > Hello,
    > > I want to create a useful function or sub whichever works great for
    > > database connection to avoid typing same code all over. Earlier I was
    > > using include file for opening and closing adoCon object and
    > > connection. Now I really want to use a function to do it.

    >
    > Where would you put that function? What is wrong with using an include
    > file?
    >
    > > Can one recommend a good example?

    >
    > > I have this sample below which does not work for me because of the
    > > mappath being wrong depending on what file under what directory calls
    > > the file with this function:

    >
    > > Sub DB_CONN

    >
    > This isn't a function, it's a subroutine.
    >
    > >   Set adoCon = Server.CreateObject("ADODB.Connection")
    > >   adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
    > > Server.MapPath("./db/web.mdb") & ";"

    >
    > "/" gets to the root. I don't know what your directory structure is but
    > something like this should work (you don't need the ending semicolon):
    >
    > adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    > "Data Source=" & Server.MapPath("/db/web.mdb")
    >
    > > End Sub

    >
    > > Sub DB_CONN_CLOSE
    > >   Set adoCon = nothing
    > > End Sub

    >
    > > Thank you for any advice.

    >
    > --
    > Microsoft MVP -- ASP/ASP.NET
    > Please reply to the newsgroup. The email account listed in my From
    > header is my spam trap, so I don't check it very often. You will get a
    > quicker response by posting to the newsgroup.


    Thanks. When I run that subroutine in a function it generates error:

    Arguments are of the wrong type, are out of acceptable range, or are
    in conflict with one another.

    If I move the code from subroutine to the function it works fine.
    Function and subroutine are both in "inc" directory but database is in
    "db" directory:

    root:
    --db
    ----db.mdb
    --inc
    ----functions.asp
    vunet, Aug 20, 2008
    #3
  4. vunet wrote:
    > On Aug 20, 12:34 pm, "Bob Barrows [MVP]" <>
    > Thanks. When I run that subroutine in a function it generates error:
    >
    > Arguments are of the wrong type, are out of acceptable range, or are
    > in conflict with one another.


    That error does not seem to be related to an inability to find the mdb
    file.

    >
    > If I move the code from subroutine to the function it works fine.


    Again ... unrelated to the mdb file location.
    Could you show us the code that works followed by the code that fails?
    I'm thinking it's a scope issue.



    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Aug 20, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Otis Mukinfus
    Replies:
    3
    Views:
    902
    Juan T. Llibre
    Jul 8, 2005
  2. Replies:
    3
    Views:
    40,168
    Roedy Green
    Nov 16, 2005
  3. Replies:
    2
    Views:
    270
  4. Mohsen Pahlevanzadeh

    Qt connect and first connect or unicode

    Mohsen Pahlevanzadeh, Sep 17, 2013, in forum: Python
    Replies:
    3
    Views:
    145
    Mohsen Pahlevanzadeh
    Sep 18, 2013
  5. Mohsen Pahlevanzadeh

    Re: Qt connect and first connect or unicode

    Mohsen Pahlevanzadeh, Sep 17, 2013, in forum: Python
    Replies:
    0
    Views:
    111
    Mohsen Pahlevanzadeh
    Sep 17, 2013
Loading...

Share This Page