DeveloperBarn Forums

Go Back   DeveloperBarn Forums > Programming & Scripting > Code Samples > Classic ASP

Discuss "Connection String & Record Set Functions" in the Classic ASP forum.

Classic ASP - Post your Classic ASP code samples here.


Reply « Previous Thread | Next Thread »  
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old March 21st, 2008, 12:07 PM
mehere's Avatar
Global Sarcasm Mistress

 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 113
Thanks: 10
Thanked 21 Times in 19 Posts
Rep Power: 1
mehere is on a distinguished road

Awards Showcase
Microsoft SQL Server Classic ASP 
Total Awards: 2

Default Connection String & Record Set Functions

I generally like to create functions for my DB Connection (Either MS SQL or Access) and Recordsets and maintain this info in an include file.

Code:
<%
'SQL Server DB Connection
function oConn()
	dim dbConn, dsnName
	set dbConn = Server.CreateObject("ADODB.Connection")
	'SERVER CONNECTION STRING
	dsnName = "Provider=SQLOLEDB;"		& _
	          "Data Source=ServerName;"	& _
	          "Initial Catalog=DB_Name;"	& _
		  "User Id=UserName;" 		& _
	          "Password=UserPassword"
	dbConn.ConnectionString = dsnName
	dbConn.Open
	set oConn = dbConn 
end function


'Access Database Connection
function oConn()
 	dim dbConn, dsnName
 	
	set dbConn = Server.CreateObject("ADODB.Connection")

	dsnName = "Provider=Microsoft.Jet.OLEDB.4.0;"                               & _
		  "Data Source=" & Server.MapPath("/database/db.mdb") & ";"   & _
		  "Persist Security Info=False"

	dbConn.ConnectionString = dsnName
	dbConn.Open
		
	set oConn = dbConn 
end function

'Create Regular RecordSet
function GetRS(sqlString)
        dim GetRS
	set GetRS = Server.CreateObject("ADODB.Recordset")
	with GetRS
		.CursorLocation = 3
		.CursorType = 3
		.ActiveConnection = oConn
		.Open sqlString
	end with
end function
%>
then to use in any ASP page, you would do the following:
Code:
strSQL = "SELECT * FROM tbl_TableName ORDER BY intID"
set rs = GetRS(strSQL)
with this, there is no need for the Server.CreateObject("ADODB.Recordset") or Server.CreateObject("ADODB.Connection") to be written on the indivudual ASP Pages. Also, should your connection string change, you only need to make a change on one page.

Comments on this post
jmurrayhead agrees: very nice
__________________
Quote of the Month:
Strife: As long as we have each other, we'll never run out of problems.

Questions to Ponder:
Should vegetarians eat animal crackers?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright © 2008 sbenj69

Last edited by mehere; March 21st, 2008 at 12:09 PM.
Reply With Quote
Sponsored Links
Reply

  DeveloperBarn Forums > Programming & Scripting > Code Samples > Classic ASP

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.

Web Directory & SEO Services
dirroot web directory


All times are GMT -4. The time now is 08:17 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Copyright © 2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46