Timing retrieval of database records using Timer Function

The code snippet below places the Timer function at the start of our code and at the end of our code and assigns the seconds returned to variables. Subtracting the StartTime from the EndTime variable will give us the time it takes to execute our code.

<%@ Language="VBScript" %>
<% Option Explicit
'Declare variables
Dim oConnection, oRecordset
Dim sSQL, sConnString, sNewsTitle, sNewsBody
Dim StartTime, EndTime, TimeIt

StartTime = Timer
%>
<html>
<head>
<title>Time our code</title>
</head>
<body>
<%
'declare SQL statement that will query the database
sSQL="SELECT HEADLINE, NEWS_STORY FROM tblNews"

'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("News.mdb")

'create an ADO connection and recordset
Set oConnection = Server.CreateObject("ADODB.Connection")
Set oRecordset = Server.CreateObject("ADODB.Recordset")

'Open the connection to the database
oConnection.Open(sConnString)

'Open the recordset object, execute the SQL statement
oRecordset.Open sSQL, oConnection

'first of all determine whether there are any records
If oRecordset.EOF Then
Response.Write("There are no news articles.")
Else
'If there are records then loop through the fields
Do While Not oRecordset.EOF
sNewsTitle= oRecordset("headline")
sNewsBody= oRecordset("news_story")
Response.Write "<p>"
Response.Write sNewsTitle & "<br />" 'insert a line break
Response.Write sNewsBody
Response.Write "</p>"
'move on to the next record
oRecordset.MoveNext
Loop
End If

'close the connection and recordset objects and free up resources
oRecordset.Close
oConnection.Close
Set oRecordset = Nothing
Set oConnection = Nothing

EndTime = Timer
TimeIt = EndTime - StartTime
Response.write "Time it took to run our code: " & TimeIt
%>

</body>
</html>

Get the best asp hosting provider from web-hosting-top.com and save up to 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Global ASP.NET Hosting Leader - Click Here