Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old April 9th, 2005, 09:12 AM
matthuxtable matthuxtable is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: UK
Posts: 165 matthuxtable User rank is Private First Class (20 - 50 Reputation Level)matthuxtable User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 10 h 34 m
Reputation Power: 4
Database Connection Strings - Sample Code

Hi,

Here is a tip for everyone who uses databases within their ASP applications regarding the database connection strings. I use this code in my applications a lot - and it comes in handy when web hosts update servers etc.

The examples here are for Access databases using the Microsoft.JET.OLEDB.4.0 connection string, but if you want it for another db and can't figure out how, just ask!

Here is what I do:

Within the global.asa file in my root website directory, I have a subprocedure Sub Application_OnStart which sets my database connection strings. Here's how:

Code:
Global.asa file
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
' Global.asa file
 
' Subprocedure fired off when the application starts
Sub Application_OnStart
 
' Application variables set the connection string
Application("strCon") = "Provider=Microsoft.JET.OLEDB.4.0; Data Source=[path to your database here - do not include the actual db file name]"
Application("strCon2") = ".mdb; JET OLEDB:Database Password=[your password here]"
 
End Sub
 
</SCRIPT>


These two strings have now set application variables. Within your application, when you connect to the database, you need to link the two connection strings together with your database name. For example:

Code:
testapp.asp file
<%
' Set the database name and connection strings
dbName="testdb"
ConnectionString = Application("strCon") & dbName & Application("strCon2")
Set dbName = nothing
 
' We would now go on to create and open ADO Database Connections etc., but I won't show that in
' this example
 
...code here...
%>


What happens is we have a generic database connection split over two different variables. strCon and strCon2 . If you look in the testapp.asp example above, I have set the database name within the page, and put this between the two Application variables:

A simple write of the ConnectionString (from the above file) would show:

Provider=Microsoft.JET.OLEDB.4.0; Data Source=[path to your database here - do not include the actual db file name]testdb.mdb; JET OLEDB:Database Password=[your password here]

Notice that the database name has been written after the [path to your database...] part and before the .mdb part, thus making a totally generic connection string which can be used to access as many different databases as you want. (Providing they use the same connection strings)

Phew! I hope this helps someone. I spent quite a while researching this when I was a newbie to ASP, and thought I would share it with you.

If you have any other questions please feel free to ask!
Comments on this post
Shadow Wizard agrees: very nice samples, would be very useful for newbies, and not only newbies!
baseballdude_ agrees: Very useful and thoughtful to take the time for all of this

Reply With Quote
  #2  
Old April 9th, 2005, 01:20 PM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 45th Plane (27000 - 27499 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,274 Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 358052 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358052 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358052 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358052 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358052 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358052 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 6 Days 13 h 10 m 23 sec
Reputation Power: 1792
great job man, keep up the good work!

Reply With Quote
  #3  
Old April 27th, 2005, 02:53 AM
Sony Sony is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Bangalore,India
Posts: 180 Sony User rank is Sergeant (500 - 2000 Reputation Level)Sony User rank is Sergeant (500 - 2000 Reputation Level)Sony User rank is Sergeant (500 - 2000 Reputation Level)Sony User rank is Sergeant (500 - 2000 Reputation Level)Sony User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 18 h 53 m 42 sec
Reputation Power: 15
why cant we use a single application variable containing the string instead of two?

Thanx
Sony

Reply With Quote
  #4  
Old April 27th, 2005, 11:56 AM
matthuxtable matthuxtable is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: UK
Posts: 165 matthuxtable User rank is Private First Class (20 - 50 Reputation Level)matthuxtable User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 10 h 34 m
Reputation Power: 4
Quote:
Originally Posted by Sony
why cant we use a single application variable containing the string instead of two?

Thanx
Sony

Hi Sony,

The way that the code works allows you to set the name of the database in the actual ASP Page, not the application variable. Yes, if you only had one database in your application, that would work, but dont forget to think about making the site bigger and getting more databases etc.

What the code does is sets the application variable up to the point where the database file name is required. I then stop here, and start the rest of the string with .mdb for the access database file name extension. The reason you have to do this is because you have not set the dbName variable when the global.asa file runs, have you?

For example, you cannot do this:

Code:
Application("strCon") = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=c:\inetpub\wwwroot\db\" & dbName & ".mdb;"
because the variable dbName has not yet been set. A simple Response.Write of the string would show the following:

Quote:
Provider=Microsoft.JET.OLEDB.4.0;Data Source=c:\inetpub\wwwroot\db\.mdb;
even after setting the dbName value to, for example, "test" in the ASP Page.

If you have any more probs, post back. I hope this explains it all!

Matthew

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > Database Connection Strings - Sample Code


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT