|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionOne of the great weaknesses of SQL Server 2000 was its inability to handle data pagination well. The solution to the problem was always to select all of your results and programmatically hide the results you did not want to show. As the user clicked Next or Previous, you would again select all of the rows and only display what the user asked for. Though the desired interface is achieved through this method, it is terribly inefficient. Why couldn't we select only the data that we wanted? If you have ever had to write a search that used " During my SQL Server 2000 days, I would search endlessly for a solution to this problem. I tried implementations of using a Throughout this process, I often theorized of a SQL Server function that could add a sequential row number to my result set and allow me to use a A few years pass by and Microsoft releases .NET which offers a partial solution to the problem. ASP.NET offers you the ability to output-cache the results of your web control. So essentially, you can select all of the rows once and as you page through the results, pull each subsequent set from the cached results. This seems to partially solve the performance problem though you are still faced with making the initial selection. But what if you want to view live changing data? As you decrease your cache time, your performance gets worse, as you increase it, your data gets old. Eventually, you fall back on your tired old method again. With the release of SQL Server 2005, Microsoft introduces the long overdue The CodeThe first step is writing your stored procedure. The SQL code for using CREATE PROCEDURE [dbo].[sp_getzipcodes]
-- Add the parameters for the stored procedure here
@start int = 0
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
Set NOCOUNT ON
SELECT TOP 20 * FROM
(
SELECT zip,city,state,latitude,longitude,timezone,dst,
ROW_NUMBER() OVER (ORDER BY zip) AS num
FROM dbo.zipcode
) AS a
WHERE num > @start
END
Now that you have your stored procedure, you will need to display the results on a web site. In our example, we use a <asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:personalConnectionString %>"
SelectCommand="sp_getzipcodes" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="start"
QueryStringField="start" DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>
Finally, you need to build your pagination controls. For this project, we accomplish this by setting a literal in our code-behind page. if ((Request.QueryString["start"] == null) |
(Request.QueryString["start"] == "0"))
{
paging.Text = "<< prev | <a href = \"?start=20\">next >></a>";
}
else
{
int start = Convert.ToInt32(Request.QueryString["start"]) + 1;
int next = Convert.ToInt32(Request.QueryString["start"]) + results;
int prev = Convert.ToInt32(Request.QueryString["start"]) - results;
if (next > max)
{
paging.Text = @"<a href = ""?start=" + prev +
@"""><< prev</a> | next >></a>";
}
else
{
paging.Text = @"<a href = ""?start=" + prev +
@"""><< prev</a> | <a href = ""?start=" +
next + @""">next >></a>";
}
}
That's about it. Download the source code to get the full project. Included in the download is the Visual Studio 2005 project, SQL Server stored procedures, and the zip code database in CSV format.
|
||||||||||||||||||||||