|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Number of rows from datareader?How do I get the number of rows a datareader has? I can tell HasRows, but
not how many. Thanks. you need to read read to the end with a counter.
a datareader (at least for sqlserver) is a forward only cursor returning data from the response stream buffer. thus you can not lnow how many rows there are until you read to the end. on a big query you can read rows while the server is still performing the query. this is also why you can not read return paramater from a sp until you've read all the result sets. -- bruce (sqlwork.com) Show quote "dew" <d**@yahoo.com> wrote in message news:uNMZN4eHGHA.2680@TK2MSFTNGP09.phx.gbl... > How do I get the number of rows a datareader has? I can tell HasRows, but > not how many. > > Thanks. > Hello dew,
No way other than iterating through the reader. Sometimes the overhead of doing this is so much that I have seen people running a count query too, before/after Read() ing the data, if needed. You might want to post this question in microsoft.public.dotnet.framework.adonet. HTH, r. Show quote > How do I get the number of rows a datareader has? I can tell HasRows, > but not how many. > > Thanks. > Thanks, all, I guess I'll have to use a dataset.
Show quote "dew" <d**@yahoo.com> wrote in message news:uNMZN4eHGHA.2680@TK2MSFTNGP09.phx.gbl... > How do I get the number of rows a datareader has? I can tell HasRows, but > not how many. > > Thanks. > As one poster has already, commented, you can still use a DataReader. Just
populate it with 2 resultsets, the first one being the count of rows returns. then use the DataReader's NextResultSet() method to switch to the one containing all the rows. Peter -- Show quoteCo-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com "dew" wrote: > How do I get the number of rows a datareader has? I can tell HasRows, but > not how many. > > Thanks. > > > this is really a bad pattern. it requires running the query twice, and if a
row is inserted/deleted between queries, the wrong answer can appear. to make the query stable you have to use exclusive lock, or select into a temp table. -- bruce (sqlwork.com) Show quote "Peter Bromberg [C# MVP]" <pbromberg@yahoo.nospammin.com> wrote in message news:0AD66EA1-E4DC-422C-B146-238267C36488@microsoft.com... > As one poster has already, commented, you can still use a DataReader. Just > populate it with 2 resultsets, the first one being the count of rows > returns. > then use the DataReader's NextResultSet() method to switch to the one > containing all the rows. > Peter > > -- > Co-founder, Eggheadcafe.com developer portal: > http://www.eggheadcafe.com > UnBlog: > http://petesbloggerama.blogspot.com > > > > > "dew" wrote: > >> How do I get the number of rows a datareader has? I can tell HasRows, >> but >> not how many. >> >> Thanks. >> >> >> Good point, Bruce, if that precision is critical to the business logic
scenario. But then, doing so would make it a "good pattern"? Cheers, Peter -- Show quoteCo-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com "Bruce Barker" wrote: > this is really a bad pattern. it requires running the query twice, and if a > row is inserted/deleted between queries, the wrong answer can appear. to > make the query stable you have to use exclusive lock, or select into a temp > table. > > -- bruce (sqlwork.com) > > > "Peter Bromberg [C# MVP]" <pbromberg@yahoo.nospammin.com> wrote in message > news:0AD66EA1-E4DC-422C-B146-238267C36488@microsoft.com... > > As one poster has already, commented, you can still use a DataReader. Just > > populate it with 2 resultsets, the first one being the count of rows > > returns. > > then use the DataReader's NextResultSet() method to switch to the one > > containing all the rows. > > Peter > > > > -- > > Co-founder, Eggheadcafe.com developer portal: > > http://www.eggheadcafe.com > > UnBlog: > > http://petesbloggerama.blogspot.com > > > > > > > > > > "dew" wrote: > > > >> How do I get the number of rows a datareader has? I can tell HasRows, > >> but > >> not how many. > >> > >> Thanks. > >> > >> > >> > > > |
|||||||||||||||||||||||