473,407 Members | 2,326 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,407 software developers and data experts.

SqlDataReader -- builtin get rowcount?

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
7 29807
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: wackyphill | last post by:
Is Set RowCount @RowCount More efficient than simply using TOP? Thanks for any input.
3
by: Dimitri Furman | last post by:
SQL Server 2000 SP3. Is it possible for the @@ROWCOUNT function to return NULL after a statement? I am troubleshooting a relatively large stored procedure with multiple SELECT statements and a...
4
by: Richard G | last post by:
I'm a database guy, so go easy on me here. :) How can I get the rowcount of the affected rows of a SQL statement from a stored procedure call? I know that "set nocount on" does not return the...
1
by: Arvind P Rangan | last post by:
Hi All, How do you get all the values of a sqldatareader if it contains multiple resultset. Using sqldatareader.nextresult and sqldatareader.read e.g. While sqldatareader.read ' If not...
6
by: Varangian | last post by:
I have an SQL Server 2005 Express Stored Procedure .. which first I select if that record exists, if it exists I update that record .. if not I insert. It then selects the record with the new data...
3
by: Jon Haakon Ariansen | last post by:
Hi, I realize that it's not possible to get the rowcount from SqlDataReader, but if you have a stored procedure where you return @@ROWCOUNT it should be possible to get the rowcount through the...
3
by: Johnny Jörgensen | last post by:
Is there any way of finding out how many rows that are returned in an System.Data.SqlClient.DataReader by a SELECT clause BEFORE you start reading the data? The SqlDataReader object doesn't seem...
3
by: Andrus | last post by:
I have DataGridView in virtual mode containing 3500 rows. In code below, assigning to RowCount value to 3500 takes 8 seconds. CPU usage goes high at this time. Stepping by F11 into user code shows...
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: 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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.