开发者

can we declare a public variable in asp using vb

开发者 https://www.devze.com 2023-04-09 23:49 出处:网络
I hav开发者_如何转开发e a established connection to my database using adoCon.Open \"Driver={SQL Server}; Server=\" & host_name & \"; Database=\" & db_name & \"; Uid=\" & user_nam

I hav开发者_如何转开发e 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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消