0

I have a established connection to my database using

adoCon.Open "Driver={SQL Server}; Server=" & host_name & "; Database=" & db_name & "; Uid=" & user_name & "; Pwd=" & password

Now i want to use this connection on all the pages of my website. How to do that? Is there anyway to make adoCon variable public so that it can be accessible from all the pages.

Thanks

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Raj Gupta
  • 260
  • 3
  • 6
  • 18

1 Answers1

1

Is it Classic ASP you're talking about?

Use the global.asa file to create an application object, such as:

Sub Application_OnStart
Application("some name") = "Your connection string"
End Sub

Then you can reuse it in all your pages, such as:

Dim rsObj,cnObj, sSQL

Set cnObj = Server.CreateObject ("ADODB.Connection")
cnObj.Open Application("some name")
Set rsObj=Server.CreateObject("adodb.recordset")
sSQL="your sql string"
With rsObj
.Open sSQL, cnObj
If Not( .BOF or.EOF ) Then
[do some stuff]                         
End If
.Close
End With
DaveSav
  • 1,364
  • 5
  • 21
  • 41
  • thanks a lot. It was very helpful. can the file global.asa file be saved with a different name or the name global.asa has some special significance. Do i need to import this file to all pages? – Raj Gupta Oct 01 '11 at 16:15
  • global.asa is an (optional) special file which should be placed in the root of your project and shouldn't be renamed. You don't need to explicitly import it to every page, it is just available to every page. Check out [link]http://www.w3schools.com[/link]/asp/asp_globalasa.asp for more info. If my answer solved your problem please accept it as the correct answer by clicking on the check mark left from the answer. Thanks. – DaveSav Oct 01 '11 at 20:01