Home > ASP Development > Writing a connection to db function

Writing a connection to db function



Hello,



I want to make a function that will connect to db when I call it and then I can use the connection it opened.

It's need to be a sub-procedure or a function?

Is it good?


Code:



<%
Sub connect()
dim DBServer
dim DBName
dim DBUser
dim DBPass
DBServer = ""
DBName = ""
DBUser = ""
DBPass = ""
Set c = Server.CreateObject("ADODB.Connection")
strConn = "Provider=SQLOLEDB; Data Source=" & DBServer & "; Initial Catalog=" & DBName & ";User Id=" & DBUser & "; Password=" & DBPass & "; Auto Translate=FALSE"
c.Open strConn
End Sub
%>



    
Guest


not sure why you want to do it this way i would personally place your connection in an include file then include those on any file that you want to peform database stuff with

Was this answer helpful ? Yes No   
Guest


yes you can do that, just make the connection public:


Code:



<%
Public objConn
Sub connect()
dim DBServer
dim DBName
dim DBUser
dim DBPass
DBServer = ""
DBName = ""
DBUser = ""
DBPass = ""
Set objConn = Server.CreateObject("ADODB.Connection")
strConn = "Provider=SQLOLEDB; Data Source=" & DBServer & "; Initial Catalog=" & DBName & ";User Id=" & DBUser & "; Password=" & DBPass & "; Auto Translate=FALSE"
objConn.Open strConn
End Sub

...
%>




then you can use objConn anywhere in the code. also, make sure to Close the

connection somewhere otherwise the database might crash with time.

Was this answer helpful ? Yes No   
Guest
 
 
Home - About Infoqu - Contact - Privacy Statement - Link to Infoqu - Bookmark Infoqu

Copyright 2007-2008 by Infoqu. All rights reserved