jeudi 15 décembre 2011
ASP: how to create a reusable function to query databases
Do you like this story?
Today we are going to create something probably out of the ordinary. We will create two reusable functions (with parameters) in order to query a database.
The first function will open a recordset and execute a query. It will accept 3 parameters: recordset name, connection and query string.
The second function will close the recordset. It will accept 1 parameter: recordset name.
In order to use the parametrised functions, we will benefit from the Execute() VBScript function.
So, before building the two functions, let's try to understand Execute().
Execute()
The Execute() function can execute anything that is passed as parameter. It is more complicate to explain than to see it in action. Let's see an example:
<%
Dim StringVar
StringVar = "response.write 1"
Execute(StringVar)
%>As a side note, consider that Execute() works like Eval()... but that's another story.
However, in order to have parameters for our two recordset functions, we are going to use Execute().
<%
function connex(rsn, conn, query)1) rsn = the recordset name;
2) conn = the connection string;
3) query = the query string.
Dim openRS
openRS = "openRS" & rsnIf we pass rsn as "Test", openRS will be "openRSTest" which will be a unique recordset name.
execute "Set " & openRS & " = Server.CreateObject(""ADODB.Recordset"")"Please note the use of double quotes.
execute openRS & ".ActiveConnection = """ & conn & """"execute openRS & ".Source = """ & query & """"execute openRS & ".CursorType = 0"
execute openRS & ".CursorLocation = 2"
execute openRS & ".LockType = 1"
execute openRS & ".Open()"
execute openRS & "_numRows = 0"end function
%>The complete function will look like:
<%
function connex(rsn, conn, query)
  Dim openRS
  openRS = "openRS" & rsn
  execute "Set " & openRS & " = Server.CreateObject(""ADODB.Recordset"")"
  execute openRS & ".ActiveConnection = """ & conn & """"
  execute openRS & ".CursorType = 0"
  execute openRS & ".CursorLocation = 2"
  execute openRS & ".LockType = 1"
  execute openRS & ".Open()"
  execute openRS & "_numRows = 0"
end function
%><%
function conclose(rsnc)Dim closeRS
closeRS = "openRS" & rsncexecute closeRS &".Close()"
execute closeRS &".ActiveConnection.close()"
execute "Set " & closeRS &".ActiveConnection = Nothing"
execute "Set " & closeRS &"= Nothing"end function
%>The complete code:
<%
function conclose(rsnc)
  Dim closeRS
  closeRS = "openRS" & rsnc
  execute closeRS &".Close()"
  execute closeRS &".ActiveConnection.close()"
  execute "Set " & closeRS &".ActiveConnection = Nothing"
  execute "Set " & closeRS &"= Nothing"
end function
%>In the page where we want to use the functions, in the head of the document, we include the connex.asp file:
<!--#include virtual="/Connections/connex.asp" --><%
Dim rsn
Dim conn
Dim query
Dim rsnc
%>Wherever we need to call the connex function, we place the following snippet:
<%
conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/path-to/database.mdb") 
query = "SELECT *  FROM table"
rsn = "table"
call connex(rsn, conn, query)
%>The result will be a recordset called "openRStable". So if we want to interact with the recordset, we need to use that name. For example when displaying results, we use:
<%
= openRStable.Fields.Item("TableField1").Value
%><%
rsnc = "table"
call conclose(rsnc)
%>Because we pass parameters to the functions, we might decide to create global variables, so that we do not need to pass the connection string every time, for example. The same for the other variables.
I think that the above exercise is quite a good starting point, but I'm sure it can be refined and improved. So, it's up to you!
If you need more information on the functions or you want to share your experience, please use the comments section below.
Happy coding.

This post was written by: Franklin Manuel
Franklin Manuel is a professional blogger, web designer and front end web developer. Follow him on Twitter


0 Responses to “ASP: how to create a reusable function to query databases”
Enregistrer un commentaire