28
February
2006
The following is an ASP script that I use to easily connect to a SQL database and do a query without having to retype connection strings and the like. It’s all contained in a Class that should be included in every page you have that calls for database interaction.
Class objDB
' Declare variables to have public scope
Public rs, cn
' Method to initialize the connection to the database
Private Sub Init
If isobject(cn) = false then
Set cn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
cn.ConnectionString = "Provider=sqloledb; Data Source=dbpath; Initial Catalog=db; User ID=username; Password=password"
cn.Open
End If
End Sub
' Method to destroy objects that were 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
' Method to execute a SQL statement
Public Function SQL(strSQL, bResults)
If isobject(cn) = false then
Init
End If
If bResults then
rs.Open strSQL, cn, 1, 1
Else
cn.Execute(strSQL)
End If
End Function
End Class
To use this code in your ASP, create the database object and call the SQL as such:
Set fooDB = New objDB
sql = "SELECT * FROM foobar"
Call fooDB.SQL(sql,true)
Posted in Coding | No Comments »
16
January
2006
Below is a function I use occasionally for dynamically including a text file into an ASP page, particularly when using query strings to select the file. Server-side Includes (SSI) are executed before ASP. This means that even if you code the query string in-line with the SSI, the text will not show up.
<%
Function IncludeText(strFilePath)
Dim fs, fname
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set fname = fs.OpenTextFile(Server.MapPath(strFilePath),1)
fname.ReadAll
fname.Close
fs.Close
fname = Nothing
fs = Nothing
End Function
%>
Simple! We’re just creating a file system object, opening the requested file, and reading all its contents to the browser. Since we made this a function, we can call it with a simple line of code.
<%
Call IncludeText(Request.QueryString("pathto/filename.txt"))
%>
If you’re not going to use query strings and just go with a standard file every time, this is probably overly done and you can just use a server-side include.
<!--#include file="./pathto/filename.txt"-->
Posted in Coding | No Comments »
7
January
2006
Have you ever had a div block which acted like a container (such as for centering) for your page content that looked absolutely wonderful on your screen, but when you went to print it, the alignment on paper was atrocious? For a quick fix, try surrounding your CSS block for your div id/class as so:
@media screen {
#container {
yadda yadda
}
}
The @media rule allows you to specify a target for rendering a specific set of CSS commands. Here we are specifying that the #container id be only applied when rendering the page on a screen. When you print the page, the browser will ignore this rule and display the content as if the containing div was not there.
(more…)
Posted in Coding | No Comments »