By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

SqlDataReader -- builtin get rowcount?

P: n/a
Is there any builtin functionality to the SqlDataReader for getting a row
count of records retrieved? Rather than Do While rdr.Read: i+=1: Loop?
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Rich wrote:
Is there any builtin functionality to the SqlDataReader for getting a row
count of records retrieved? Rather than Do While rdr.Read: i+=1: Loop?

No, since the datareader only fetches rows as you ask for them
(rdr.Read) it does not know how many there will be. If you use the data
adapter, then you will be able to get a row count since it fetches the
rows. Otherswise, make to calls, on to get the rowcount and one to get
the data.

Chris
Nov 21 '05 #2

P: n/a
Hello Rich
I donot think there is a property that can give you the row count right away
just like the record count property of ADODB.Recordset
Probably, You can load the data into a dataset and use datarow collection to
get the row count.
Let me know if you find a better solution
Thanks
Deepak

"Rich" wrote:
Is there any builtin functionality to the SqlDataReader for getting a row
count of records retrieved? Rather than Do While rdr.Read: i+=1: Loop?

Nov 21 '05 #3

P: n/a
Thanks all for your replies. Basically, I have a sql table that contains
columns of column names for other tables. Col1 may contain 90 rows where
each cell contains column names for tbl1, Col2 contains 40 rows of not null
data for the columns in tbl2, ...

I am using a datareader to read the not null values in each column
individually, and I loop through the datareader to add sqlInsertCommand
Params (for my Insert SP in sql server which has the same number of params).
So I am stuck with using

i += 1 '--i is the column ordincal for tlb1 and tbl2 and the row in the
columnNames
'--table
'--rdr reads the rows in the ColumnNames table
While rdr.Read
str1 = rdr.GetString(0)
'--this is a Text field for tbl1 and tbl2
If ((stp = "cd" Or stp = "qa") And i = 2) Or (stp = "qa" And i = 8) Then
cmdNcalInsert.Parameters.Add(str1, SqlDbType.NText, 16,
str1.Substring(1, str1.Length - 1))
ElseIf str1 = "@RowNum" Then
cmdNcalInsert.Parameters.Add(str1, SqlDbType.Int, 4, str1.Substring(1,
str1.Length - 1))
Else
cmdNcalInsert.Parameters.Add(str1, SqlDbType.NVarChar, 20,
str1.Substring(1, str1.Length - 1))
End If
i += 1
End While

"Rich" wrote:
Is there any builtin functionality to the SqlDataReader for getting a row
count of records retrieved? Rather than Do While rdr.Read: i+=1: Loop?

Nov 21 '05 #4

P: n/a
Rich,

This kind of procedures you can nicely do with a progressbar. And of couse
do you than have to set the steps of the progressbar yourself. (something
you do now)

See this sample on our website
http://www.windowsformsdatagridhelp....6-fc0d5c470f53

I hope this gives an idea.

Cor
Nov 21 '05 #5

P: n/a
Hi,

If finding out the number of rows is really important
executenonquery will return the number of rows affected. You still will
have use executereader to get a datareader. Running the query twice will
impact performance.

Ken
---------------

"Rich" wrote:
Is there any builtin functionality to the SqlDataReader for getting a row
count of records retrieved? Rather than Do While rdr.Read: i+=1: Loop?

Nov 21 '05 #6

P: n/a
Basically, I have a table in sql server that has 4 columns/fields. Each
column represents a different sql server table. I load parameter names in
each column

tbl1 tbl2 tbl3 tbl4
@ID @ID @ID @ID
@fld1tbl1 @fld1tbl2 @fld1tbl3 @fld1tbl4
@fld2tbl1 @fld2tbl2 @fld2tbl3 @fld2tbl4
.... @lastfid ... ....
.... @lastfld ...
@lastfld ...

@lastfld

Each table has different fields and different number of fields. I have 4
separate Insert Into SPs for each table that take the respective parameters.
The params are averageing from 30 params for one table to 180 params for
another table. Too many params for an array of params (already tried - works
but way too messy). So I decided to list my params for each table in a
Params Table. I use the datareader to read each column for each table

strSql = "Select col1 From tblParams Where col1 Is Not Null"

for tbl1 for example. I loop through the data reader and load up my params.
I can't even remember why I needed the count of rows. But I do need to know
what row I am on because for col1 row3 the param has to be ntext, the rest of
the rows are nvarchar(100), lastfld is int, for example.

Rich

"Ken Tucker [MVP]" wrote:
Hi,

If finding out the number of rows is really important
executenonquery will return the number of rows affected. You still will
have use executereader to get a datareader. Running the query twice will
impact performance.

Ken
---------------

"Rich" wrote:
Is there any builtin functionality to the SqlDataReader for getting a row
count of records retrieved? Rather than Do While rdr.Read: i+=1: Loop?

Nov 21 '05 #7

P: n/a
Thanks for the example. This will be very useful.

Rich

"Cor Ligthert [MVP]" wrote:
Rich,

This kind of procedures you can nicely do with a progressbar. And of couse
do you than have to set the steps of the progressbar yourself. (something
you do now)

See this sample on our website
http://www.windowsformsdatagridhelp....6-fc0d5c470f53

I hope this gives an idea.

Cor

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.