Connecting Tech Pros Worldwide Help | Site Map

Connect to Database Function

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 20th, 2008, 02:45 PM
vunet
Guest
 
Posts: n/a
Default Connect to Database Function

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.

  #2  
Old August 20th, 2008, 04:45 PM
Bob Barrows [MVP]
Guest
 
Posts: n/a
Default Re: Connect to Database Function

vunet wrote:
Quote:
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?
Quote:
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.
Quote:
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")

Quote:
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.


  #3  
Old August 20th, 2008, 05:15 PM
vunet
Guest
 
Posts: n/a
Default Re: Connect to Database Function

On Aug 20, 12:34*pm, "Bob Barrows [MVP]" <reb01...@NOyahoo.SPAMcom>
wrote:
Quote:
vunet wrote:
Quote:
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?
>
Quote:
Can one recommend a good example?
>
Quote:
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:
>
Quote:
Sub DB_CONN
>
This isn't a function, it's a subroutine.
>
Quote:
* 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")
>
Quote:
End Sub
>
Quote:
Sub DB_CONN_CLOSE
* Set adoCon = nothing
End Sub
>
Quote:
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
  #4  
Old August 20th, 2008, 05:46 PM
Bob Barrows [MVP]
Guest
 
Posts: n/a
Default Re: Connect to Database Function

vunet wrote:
Quote:
On Aug 20, 12:34 pm, "Bob Barrows [MVP]" <reb01...@NOyahoo.SPAMcom>
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.
Quote:
>
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.


  #5  
Old August 21st, 2008, 03:35 AM
iahamed via WebmasterKB.com
Guest
 
Posts: n/a
Default Re: Connect to Database Function

<%
Dim vPath, pPath, ConString

vPath = "admin\carstore.mdb" 'use this one if database is in root of cart
folder
pPath = Server.MapPath( vPath )

ConString = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & pPath & ";" &
"JET OLEDB:Database Password= "
%>

or use the other what Bob Barrows said.

It seems you are not trying.


vunet wrote:
Quote:
Quote:
Quote:
Hello,
I want to create a useful function or sub whichever works great for
>[quoted text clipped - 38 lines]
Quote:
>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
--
Message posted via WebmasterKB.com
http://www.webmasterkb.com/Uwe/Forums.aspx/asp/200808/1

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.