Calling Stored Procedure through the Recordset Object

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 3
September 01, 1999
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement


I discovered how to call stored procedures with less than 5 lines of code. I
recently had to display a webpage that was based on a table that contained several
calculated totals. The reason I had this summary/reporting table is a lot of data
was calculated and allowed for almost anyway of displaying stat's about the data. 
Here is a brief summary of what I did. I created a table in SQL 7 that held the
data.

Every 4 hours the table was refreshed with new data using a Stored
Procedure. Then I created another Stored procedure that was being called
from the ASP page. Without writing a book on what I did here is a 10,000 ft
level overview. Hope this demo shows another way of using Stored Procedures to
retrieve data a Data Source using ASP. Stored Procedures are no more than an SQL
statement retreving data. Why hold that Query inside an ASP page when you can use
the Database server to run your Query's


Step 1



Create a Reporting Table that will be populated with the totals. This is
the SQL Generated Script that will created the table. This table holds all the
calculated data that is based on Raw numbers that are inputted.


<p>CREATE TABLE [dbo].[test2] (
[NickName] [char] (50) NULL ,
[TotalPoints] [int] NULL ,
[Count_NickName] [char] (50) NULL ,
[AvgScore] [decimal](18, 1) NULL ,
[NumOfStrikes] [decimal](18, 0) NULL ,
[WinLossPercentage] [decimal](18, 2) NULL ,
[Wins] [int] NULL ,
[Loses] [int] NULL
) ON [PRIMARY]
GO
</p>

Step 1 1/2 Here is some data to load in the db.

NickNameTotalPointsCount_NickNameAvgScoreNumOfStrikesWinning_PercentageWinsLoses
NickName16111384.42852.97365
NickName2445944.72857.455440
NickName3419974.33549.484849
NickName4383834.62451.814340
NickName5337744.62054.054034
NickName6329794.21455.74435
NickName73248141645.683744


Step 2

Create Stored Procedure that will be called from an ASP page. Ok this is
just a normal select statement but I'm convinced that put code where it will run the
fastest. On the database server, you only have to call the stored Procedure and
return the data. Instead of parsing the query and then running the statement.



CREATE PROCEDURE [TestStoredProcedure] AS
SELECT test2.NickName, test2.TotalPoints, test2.Count_NickName, test2.AvgScore,
test2.NumOfStrikes, test2.WinLossPercentage, test2.Wins,test2.Loses
FROM test2
ORDER BY test2.NickName;
GO


Step 3

Create an ASP page. The normal stuff, connection string, recordset object
etc.. here is the code


<% @language="vbscript" %>
<%
dim conn
dim strconn
dim rs
dim strql

'Normal Connection String
strconn= "Driver={SQL
Server};SERVER=127.0.0.1;UID=LoginID;PWD=password;DATABASE=some_db"
set conn = server.createobject("adodb.connection")
conn.open strconn


'This is a local variable that holds the variable the execute the stored proc.
<font color="#FF0000">strsql = "Exec TestStoredProcedure"
set rs = server.createobject("adodb.recordset")
rs.open strsql, conn

%>
<html>

<head>
<title>Example</title>
</head>

<body>
<% rs.movefirst %>
<table BORDER="1" width="80%">
<tr></p>

<p><td colspan="8"><center><b><strong>This section writes out
the Names of the fields in the Recordse</strong>t</center></b></td></tr>
<tr>
<% For Each Field In RS.Fields %>


<th>
<%
response.write "<b>" & Field.name & "</b>"
%>
</th>
<% Next %>
</tr>
<tr></p>

<td colspan="8"><center>This section writes out
the DATA of the fields in the Recordset</strong></center></b></td></tr>
<% Do While Not RS.EOF %>
<tr>
<% For Each Field In RS.Fields %>
<td ALIGN="center">
<%
If IsNull(Field) Then
Response.Write ""
Else
Response.Write Field.Value
End If %>
</td>
<% Next
RS.MoveNext %>
<% Loop %>
</table>
</body>
</html>
<%
'Close all objects and set to nothing to keep resources free!
rs.close
conn.close
set rs = nothing
set conn = nothing
%></p>

blog comments powered by Disqus
DATABASE CODE ARTICLES

- Deployment of the MobiLink Synchronization M...
- MobiLink Synchronization Wizard in SQL Anywh...
- Finding Matching Records in Data Access Pages
- Using the AccessDataSource Control in VS 2005
- A Closer Look at ADO.NET: The Command Object
- A Closer Look at ADO.NET: The Connection Obj...
- Using ADO to Communicate with the Database, ...
- Code Snippets: Counting Records
- Constraints In Microsoft SQL Server 2000
- Multilingual entries into a DB and to be dis...
- Two combos, one textbox example
- ADO Recordset Paging
- SQL Server Database Creator - .NET Version
- Getting A List of Tables From SQL Server
- Discussion & Listserv Module by Mike Eck...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials