Using stored procedure
This sample demonstrate how to use stored procedure to fetch only needed data to display for the current page
| Order ID | Order Date | Customer ID | Ship Address |
|---|---|---|---|
| 11060 | 4/30/1998 12:00:00 AM | FRANS | Via Monte Bianco 34 |
| 11061 | 4/30/1998 12:00:00 AM | GREAL | 2732 Baker Blvd. |
| 11062 | 4/30/1998 12:00:00 AM | REGGC | Strada Provinciale 124 |
| 11063 | 4/30/1998 12:00:00 AM | HUNGO | 8 Johnstown Road |
| 11064 | 5/1/1998 12:00:00 AM | SAVEA | 187 Suffolk Ln. |
| 11065 | 5/1/1998 12:00:00 AM | LILAS | Carrera 52 con Ave. Bolívar #65-98 Llano Largo |
| 11066 | 5/1/1998 12:00:00 AM | WHITC | 1029 - 12th Ave. S. |
| 11067 | 5/4/1998 12:00:00 AM | DRACD | Walserweg 21 |
| 11068 | 5/4/1998 12:00:00 AM | QUEEN | Alameda dos Canàrios, 891 |
| 11069 | 5/4/1998 12:00:00 AM | TORTU | Avda. Azteca 123 |
| 11070 | 5/5/1998 12:00:00 AM | LEHMS | Magazinweg 7 |
| 11071 | 5/5/1998 12:00:00 AM | LILAS | Carrera 52 con Ave. Bolívar #65-98 Llano Largo |
| 11072 | 5/5/1998 12:00:00 AM | ERNSH | Kirchgasse 6 |
| 11073 | 5/5/1998 12:00:00 AM | PERIC | Calle Dr. Jorge Cash 321 |
| 11074 | 5/6/1998 12:00:00 AM | SIMOB | Vinbæltet 34 |
| 11075 | 5/6/1998 12:00:00 AM | RICSU | Starenweg 5 |
| 11076 | 5/6/1998 12:00:00 AM | BONAP | 12, rue des Bouchers |
| 11077 | 5/6/1998 12:00:00 AM | RATTC | 2817 Milton Dr. |
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<PagedList<Order>>" %>
<%@ Import Namespace="Webdiyer.Mvc2Demo.Models" %>
<%@ Import Namespace="Webdiyer.WebControls.Mvc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Using stored procedure
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Using stored procedure</h2>
<div>This sample demonstrate how to use stored procedure to fetch
only needed data to display for the current page</div><br />
<table width="98%">
<tr><th>Order ID</th><th>Order Date</th><th>Customer ID</th><th>Ship Address</th></tr>
<%foreach(Order od in Model)
{
%>
<tr><td><%=od.OrderID %></td><td><%=od.OrderDate.ToString() %></td>
<td><%=od.CustomerID %></td><td><%=od.ShipAddress %></td></tr>
<%
} %>
</table>
<%=Html.Pager(Model, new PagerOptions { PageIndexParameterName = "id",
NumericPagerItemCount=15})%>
</asp:Content>
CREATE PROCEDURE P_GetPagedOrders2005
(@startIndex INT,
@endindex INT
)
AS
begin
WITH orderList AS (
SELECT ROW_NUMBER() OVER (ORDER BY O.orderid DESC)AS Row, O.*
from orders O)
SELECT *
FROM orderlist
WHERE Row between @startIndex and @endIndex
end
public ActionResult StoredProcedure(int? id)
{
using (var db = new MvcPagerDemoDataContext())
{
//start record index
int startIndex = id.HasValue && id.Value > 1 ? (id.Value - 1) * 20 + 1 : 0;
//get data for display in current page using stored procedure
List<Order> orders = db.GetPagedOrders(startIndex, startIndex + 20).ToList();
//total record count to page through
int totalOrders = db.Orders.Count();
//Create PagedList<T> using constructor
PagedList<Order> pagedOrders = new PagedList<Order>(orders, id ?? 1, 20, totalOrders);
return View(pagedOrders);
}
}