473,854 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 29876
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 sqlInsertComman d
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.P arameters.Add(s tr1, SqlDbType.NText , 16,
str1.Substring( 1, str1.Length - 1))
ElseIf str1 = "@RowNum" Then
cmdNcalInsert.P arameters.Add(s tr1, SqlDbType.Int, 4, str1.Substring( 1,
str1.Length - 1))
Else
cmdNcalInsert.P arameters.Add(s tr1, SqlDbType.NVarC har, 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
11012
by: wackyphill | last post by:
Is Set RowCount @RowCount More efficient than simply using TOP? Thanks for any input.
3
10929
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 couple of INSERTs into table variables. Immediately after each statement I save the value returned by @@ROWCOUNT to a local variable. That information eventually is passed back to the client via one output parameter, for all statements in the...
4
23563
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 number of affected rows to the client, so I would assume that "set nocount off" sends the number of affected rows to the client, and therefore, is available programatically. If so, how to get that rowcount? Thanks, Richard
1
3266
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 sqldatareader then sqldatareader.nextresult
6
3065
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 updated or inserted... however the .NET SqlDataReader always takes the first query results (which the query that first ran to check if that record exists) .. How can I take the second query results?. I can use the Try Catch but that will...
3
4214
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 returnvalue parameter to sqldatareader, shouldn't it??? Here is an example: SqlDataReader dr = null; SqlConnection sc = new SqlConnection(_conn);
3
25956
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 to have any properties that tell that like Rows.Count etc... The RowsAffected property always returns -1 when it's a SELECT clause. It only works with UPDATE, INSERT and DELETE.
3
5759
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 few celltemplate property getters and combobox/datecombo constructor calls without database access which does not take a lot of time. Debug output (below) shows lot of messages Stepping over non-user code. Running in release mode from...
0
9903
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11041
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10692
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10766
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9525
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5753
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5946
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.