472,143 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

turning Resultsets Using Sql Server Stored Procedures...

Stored procedures are faster and more efficient than in-line SQL
statements. In this article we will look at two SQL Server stored
procedures; one using an input parameter and one not, and see how to
call them from an ASP.Net page

Every modern database system has a stored procedure language. SQL
Server is no different and has a relatively sophisticated and easy to
use system. This article will not attempt to go into depth in
explaining SQL Server stored procedure programming - there are whole
books devoted to the subject. Instead we will provide a glimpse at how
easy it is to perform some relatively simple tasks that we .Net
programmers need to do every day.

In this example we will use use two stored procedures. The first will
bring back a comlete list of all the author last names from the
authors table in the Pubs database. The lastnames will be placed in a
DropDownList. The second procedure will accept as an input parameter
the author lastname selected in the DropDownList and return the entire
row(s) of data for that author last name. This information will be
place in a DataGrid.

First the .aspx page which contains a label telling the user to select
a name from the dropdown, the DropDownList containing the last names,
and a button to execute the sub-routine which will fill the DataGrid
with the row of data for the selected last name. Following is the code
for the .aspx page.


At this point we take a look at the stored procedures before finishing
up with the code-behind page. The first stored procedure returns all
of the last names from the authors table and is really quite simple.
It is as follows:

select distinct au_lname from authors;
All we have done is create a procedure, give it a name, use the AS
keyword, followed by our select statement. That is all that is needed.
When ADO.Net executes that procedure name it will receive back a
complete resultset just as it would have with an in-line SQL

The next procedure is a little different in that it accepts an input
parameter which will appear in the WHERE clause so that only specific
rows are returned. That procedure is a follows.

CREATE PROCEDURE GetAuthorsByLastName(@au_lname varchar(20))
select * from authors where au_lname=@au_lname;
This time we have again created a procedure, with a name, but with an
input parameter added parenthetically after the procedure name.
Parameters are denoted by the @ sign. I used @au_lname (the same as
the column name I am comparing to), but I could have used @xxx if I
had wanted to. We then have our select statement but with a WHERE
clause to bring back only rows where the author last name is equal to
the last name we selected in our DropDownList. We will see how that
works next in the code-behind file.

As usual, all the work gets done in the code-behind file. It is shown
below. We fill the DropDownList in the Page_Load event. Notice the
line strSql = "EXECUTE GetAuthorLastNames". Instead of an in-line SQL
statement we simply are telling ADO to execute our first stored
procedure. Four lines below that we set dataReader =
objCmd.ExecuteReader and we have our result set. From there it is just
a matter of setting the DropDownList's datasource to the DataReader.

It is in the btnGetAuthor_Click event that we respond to the button
being clicked on the .aspx page after an author last name has been
selected. The operative line in that event is:

strSQL = "EXECUTE GetAuthorsByLastName '" &
ddlAuthor.SelectedItem.Text & "'"
Here our SQL is to execute our second stored procedure, but also
passing in the input variable which is the author last name selected
in the DropDownList. After that we again get our DataReader and then
set the datasource of our DataGrid.

Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient

Public Class SqlStoredProc : Inherits System.Web.UI.Page

Protected ddlAuthor As System.Web.UI.WebControls.DropDownList
Protected dtgAuthors As System.Web.UI.WebControls.DataGrid
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim dataReader As SqlDataReader
Dim strSql As String

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Handles MyBase.Load
If Not IsPostBack Then
Dim objConn As SqlConnection
Dim dataReader As SqlDataReader

objConn = New SqlConnection(ConfigurationSettings.AppSettings.Ge t("ConnectionString"))
strSql = "EXECUTE GetAuthorLastNames"
objCmd = New SqlCommand(strSql, objConn)
dataReader = objCmd.ExecuteReader
With ddlAuthor
.DataSource = dataReader
.DataTextField = "au_lname"
.DataValueField = "au_lname"
End With
End Try
End If
End Sub

Public Sub btnGetAuthor_Click(sender as Object, e As EventArgs)
objConn = New SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))

strSQL = "EXECUTE GetAuthorsByLastName '" &
ddlAuthor.SelectedItem.Text & "'"
objCmd = New SqlCommand(strSQL, objConn)
dataReader = objCmd.ExecuteReader()
'Bind to DataGrid
dtgAuthors.DataSource = dataReader
End Try
End Sub

End Class
I hope you have learned how easy it is to write simple SQL Server
stored procedures to return data to your .Net programs. As you may
guess, it is just as simple to do INSERTs, UPDATEs, and DELETEs as
AMBER [MCSD.NET MCAD.NET] http://www.dedicatedsolutions.co.uk
Nov 17 '05 #1
0 2496

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Tor Heigre | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.