473,395 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

ADO.NET and SQL Server Data Paging

I had to built a custom data paging control for my asp.net app and SQL
Server.

This is what I'm doing:

1. do a SELECT to a SqlDataReader
2. create a DataTable with data from GetSchema()
3. loop trough the datareader using .Read() to the point I want to start
4. add data to DataTable with max of [pageSize] records
5. close the data reader and return

The question is:

Isn't looping reader.Read() doing nothing to go to start point where I
want start getting data too expensive? Although DataReader is fast,
won't ".Read()" get the row data from the server to the client? If so,
if I need to get data from record 10000 ahead, it is actually reading
all 10 thousand rows of data, so it is a big waste of resources.

Maybe someone who knows more about the internals of the DataReader can
answer this.

Since SQL Server doesn't support "LIMIT" like keywords, if there is a
better way to page data in sql server, I would like to know.

Thanks.
Nov 19 '05 #1
6 4266
Provided you are using an incrementing number, like an Identity column, you
can store first and last and then use a DataSet and bind (avoid reader to
fill a DataTable and make the code a bit simpler). If you really like using a
Reader (MS does under the hood), it is not a problem.

Using the "pointers" you are storing, you can easily request data from that
point. If you like persisting the data rather than requesting each time, read
the DataTable as XML and pull the subset you want to paint on the screen.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
"Natan Vivo" wrote:
I had to built a custom data paging control for my asp.net app and SQL
Server.

This is what I'm doing:

1. do a SELECT to a SqlDataReader
2. create a DataTable with data from GetSchema()
3. loop trough the datareader using .Read() to the point I want to start
4. add data to DataTable with max of [pageSize] records
5. close the data reader and return

The question is:

Isn't looping reader.Read() doing nothing to go to start point where I
want start getting data too expensive? Although DataReader is fast,
won't ".Read()" get the row data from the server to the client? If so,
if I need to get data from record 10000 ahead, it is actually reading
all 10 thousand rows of data, so it is a big waste of resources.

Maybe someone who knows more about the internals of the DataReader can
answer this.

Since SQL Server doesn't support "LIMIT" like keywords, if there is a
better way to page data in sql server, I would like to know.

Thanks.

Nov 19 '05 #2
You can think about following query:

SELECT TOP PAGE_SIZE * FROM TABLE_NAME WHERE KEY_ID > last_ID ORDER BY KEY_ID

HTH

Elton Wang
el********@hotmail.com

"Natan Vivo" wrote:
I had to built a custom data paging control for my asp.net app and SQL
Server.

This is what I'm doing:

1. do a SELECT to a SqlDataReader
2. create a DataTable with data from GetSchema()
3. loop trough the datareader using .Read() to the point I want to start
4. add data to DataTable with max of [pageSize] records
5. close the data reader and return

The question is:

Isn't looping reader.Read() doing nothing to go to start point where I
want start getting data too expensive? Although DataReader is fast,
won't ".Read()" get the row data from the server to the client? If so,
if I need to get data from record 10000 ahead, it is actually reading
all 10 thousand rows of data, so it is a big waste of resources.

Maybe someone who knows more about the internals of the DataReader can
answer this.

Since SQL Server doesn't support "LIMIT" like keywords, if there is a
better way to page data in sql server, I would like to know.

Thanks.

Nov 19 '05 #3
Take a look at:

http://www.aspfaq.com/show.asp?id=2120

Rick Sawtell
MCT, MCSD, MCDBA

Nov 19 '05 #4
Check out http://www.aspfaq.com/show.asp?id=2120 for various paging methods
Personally, I build parameterized dynamic SQL on the client side using the
query technique below. The primary key (or unique columns) are specified in
the WHERE and ORDER BY clauses.

use Northwind

--first page
SELECT TOP 10 OrderID, CustomerID, OrderDate
FROM Orders
ORDER BY OrderID ASC

--next page
SELECT TOP 10 OrderID, CustomerID, OrderDate
FROM Orders
WHERE OrderID > 10257 --last OrderID from currently displayed page
ORDER BY OrderID ASC

--prev page
SELECT OrderID, CustomerID, OrderDate
FROM
(
SELECT TOP 10 OrderID, CustomerID, OrderDate
FROM Orders
WHERE OrderID < 10258 --first OrderID from currently displayed page
ORDER BY OrderID DESC
) AS a
ORDER BY OrderID ASC

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Natan Vivo" <nv***@terra.com.br> wrote in message
news:u8**************@TK2MSFTNGP12.phx.gbl...
I had to built a custom data paging control for my asp.net app and SQL
Server.

This is what I'm doing:

1. do a SELECT to a SqlDataReader
2. create a DataTable with data from GetSchema()
3. loop trough the datareader using .Read() to the point I want to start
4. add data to DataTable with max of [pageSize] records
5. close the data reader and return

The question is:

Isn't looping reader.Read() doing nothing to go to start point where I
want start getting data too expensive? Although DataReader is fast, won't
".Read()" get the row data from the server to the client? If so, if I need
to get data from record 10000 ahead, it is actually reading all 10
thousand rows of data, so it is a big waste of resources.

Maybe someone who knows more about the internals of the DataReader can
answer this.

Since SQL Server doesn't support "LIMIT" like keywords, if there is a
better way to page data in sql server, I would like to know.

Thanks.

Nov 19 '05 #5
You can use this

Dim oConnection As New SQLConnection("Provider...
oConnection.Open

Dim oDataAdapter As New SqlDataAdapter("SELECT ...", oConnection)

Dim oDataSet As DataSet = New DataSet

oDataAdapter.Fill(oDataSet, CurrentPageSize, PageSize, mstrTableName)
If you have 400 records and you need the first 100 records then
CurrentPageSize = 0 and PageSize = 100
If you need the next 100 records then CurrentPageSize = 100 and PageSize =
100

Good luck
Marc R.

"Natan Vivo" <nv***@terra.com.br> a écrit dans le message de news:
u8**************@TK2MSFTNGP12.phx.gbl...
I had to built a custom data paging control for my asp.net app and SQL
Server.

This is what I'm doing:

1. do a SELECT to a SqlDataReader
2. create a DataTable with data from GetSchema()
3. loop trough the datareader using .Read() to the point I want to start
4. add data to DataTable with max of [pageSize] records
5. close the data reader and return

The question is:

Isn't looping reader.Read() doing nothing to go to start point where I
want start getting data too expensive? Although DataReader is fast, won't
".Read()" get the row data from the server to the client? If so, if I need
to get data from record 10000 ahead, it is actually reading all 10
thousand rows of data, so it is a big waste of resources.

Maybe someone who knows more about the internals of the DataReader can
answer this.

Since SQL Server doesn't support "LIMIT" like keywords, if there is a
better way to page data in sql server, I would like to know.

Thanks.

Nov 19 '05 #6
Cowboy (Gregory A. Beamer) - MVP wrote:
Provided you are using an incrementing number, like an Identity column, you


No, I am not. That is why I needed to create my own pagination. There is
no way to determine what is beeing paginated neither the type of the
field in order by.. It may be text, may be number...

Does anyone know if the reader.Read() will actually read the data? if
yes, than probably there is no better way to do this. My inplementation
is pretty like using dataset, saving that i don't need to instantiate a
DataAdaper or neither a DataSet..

Thanks.
Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Justin Lazanowski | last post by:
Cross posting this question on the recommendation of an I have a .NET application that I am developing in C# I am loading information in from a dataset, and then pushing the dataset to a grid,...
2
by: Rathtap | last post by:
I am displaying data in a bound datagrid. Is there a way to provide paging through client-side script? That is, without re-loading the page. I am willing to have the initial overhead of bringing in...
8
by: Greg Lyles | last post by:
Hi all, I'm trying to develop an ASP.NET 2.0 website and am running into some real problems with what I thought would be a relatively simple thing to do. In a nutshell, I'm stuck on trying to...
1
by: rbg | last post by:
I am using derived tables to Page data on the SQL Server side. I used this link as my mentor for doing paging on the SQL Serverhttp://msdn2.microsoft.com/en-us/library/ms979197.aspx I wanted to...
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
3
by: sloan | last post by:
What's a good "go to" article on GridView with Paging.. and options with Sql Server 2005. (2000 does not necessarily need to be supported). I've googled, but am getting too many hits to wade...
5
by: Donald Adams | last post by:
Hi, I will have both web and win clients and would like to page my data. I could not find out how the datagrid control does it's paging though I did find some sample code that says they do it...
8
by: Donald Adams | last post by:
I heard in an online video by Scott Gu, that there is an option to have the asp.net datagrid in .net 2.0 do the paging in SQL Server 2005 rather than pull all the data from the database and do it...
2
by: Ilyas | last post by:
Hi all I need to implmenet paging across different tables. The tables all have a different name eg Data01, data02 data03 etc, however they are columns which are common to each table, but each...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.