Net-informations.com
SiteMap  | About    

ASP.NET Stored Procedures

A stored procedure is a group of Transact-SQL statements compiled into a single execution plan. You can write stored procedure and save it into the database. If you are in a situation to using the same query over and over again, it is better to create a stored procedure instead. A sample Stored Procedure is given below :

  

		CREATE PROCEDURE SPAUTHORS
		AS
		SELECT AU_LNAME FROM AUTHORS
		GO

The above code create a procedure named as 'SPAUTHORS' and it execute SQL statement that select all authors last name from authors table from the PUB database. Coding business logic into a single stored procedure offers a single point of control for ensuring that business rules are correctly enforced and improve performance.

Stored procedures can also shield users from needing to know the details of the tables in the database, users unable to access the tables directly and they can just execute the stored procedures only. The command Object in ADO.NET provides a number of Execute methods that can be used to perform the SQL queries in a variety of fashions.

To call a stored procedure from ASP.NET , set the CommandType of the Command object to Stored Procedure.

  command.CommandType = CommandType.StoredProcedure;

From the following source code you can see how to call a stored procedure from an ASP.NET application.

Default.aspx

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
	<title>Untitled Page</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
	<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
	<br />
		<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
	<br />
	<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
	</div>
	</form>
</body>
</html>


Click the following links to see full source code

C# Source Code
VB.NET Source Code
default.aspx.cs
default.aspx.vb





net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.