+ Reply to Thread
Results 1 to 1 of 1

Thread: Connection String & Record Set Functions

  1. #1
    Super Sarcasm Mistress mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere's Avatar
    Join Date
    Mar 2008
    Location
    Wide Awake In Dreamland
    Posts
    830
    Rep Power
    8

    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.
    Last edited by mehere; March 21st, 2008 at 01:09 PM.
    Quote of the Month:
    INSIGHT: When the going gets tough, the tough get going. The smart left a long time ago.

    Questions to Ponder:
    Are people more violently opposed to fur rather than leather because it's much easier to harass rich women than motorcycle gangs?

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

    Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.

+ Reply to Thread

Similar Threads

  1. Replies: 3
    Last Post: June 18th, 2008, 09:55 AM
  2. Update sql connection string in web config
    By peebman2000 in forum .NET Development
    Replies: 5
    Last Post: April 18th, 2008, 01:23 PM
  3. Select Top 1 Unique Record
    By BLaaaaaaaaaarche in forum SQL Development
    Replies: 13
    Last Post: April 17th, 2008, 10:12 AM
  4. String Functions Listed By Name
    By sbenj69 in forum Microsoft Access
    Replies: 0
    Last Post: April 3rd, 2008, 11:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO