<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>codevalve &#187; classes</title>
	<atom:link href="http://codevalve.com/tag/classes/feed/" rel="self" type="application/rss+xml" />
	<link>http://codevalve.com</link>
	<description></description>
	<lastBuildDate>Mon, 15 Nov 2010 18:46:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Database Connection Through VBScript Classes Part 1</title>
		<link>http://codevalve.com/database-connection-through-vbscript-classes-part-1/</link>
		<comments>http://codevalve.com/database-connection-through-vbscript-classes-part-1/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 06:08:34 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[article]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[vbscript]]></category>

		<guid isPermaLink="false">http://codevalve.com/?p=3</guid>
		<description><![CDATA[Are you tired of writing the code to connect to a database over and over again?
I've grown very tired of it. I decided it was time to build a reusable class to hook up the connection and record set objects I use in almost all of my projects.]]></description>
			<content:encoded><![CDATA[<p>Are you tired of writing the code to connect to a database over and over again?<br />
I&#8217;ve grown very tired of it. I decided it was time to build a reusable class to hook up the connection and recordset objects I use in almost all of my projects.</p>
<p>The concept I came to love is a VBScript class.</p>
<p>Here is the code for the class.</p>
<pre class="vb" name="code">Class DataBaseFunctions

' Declare variables to have public scope.

Public rs
Public cn

'Method to initialize connection to database

Private Sub Init

if isobject(cn) = false then

Set cn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")

cn.ConnectionString = Application("DB_CONN")
cn.Open

end if

End Sub

' Method to destory objects created.

Public Sub Destroy

if isobject(rs) = true then
rs.Close
Set rs = nothing
end if

if isobject(cn) = true then
cn.Close
Set cn = nothing
end if

End Sub

End Class</pre>
<p>Now I only need one line of code on my ASP page to initialize my database connection. Notice the use of the Application object to store the connection string. This has always been a convieniant way to store global information and make it eaiser to change later.<br />
Add this code to your global.asa in the Application_OnStart() sub to set the Application(&#8220;DB_CONN&#8221;) variable equal to your database DSN (in this case) or a DSN-less connection string.</p>
<pre class="vb" name="code">Application("DB_CONN") = "DSN=cvDatabase;"</pre>
<p>Here is how you would use the DataBaseFunctions class in your ASP page :</p>
<pre class="html" name="code">&lt;HTML&gt;
&lt;HEAD&gt;
&lt;!-- #INCLUDE File="class_DataBaseFunctions.asp" --&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;%
Set db = new DataBaseFunction

db.Init
Set db.rs = db.cn.Execute("SELECT * FROM tblUser WHERE UserID = 10")

Response.Write(db.rs.Fields(1).value)

db.Destroy

Set db = Nothing
%&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://codevalve.com/database-connection-through-vbscript-classes-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.297 seconds -->

