473,486 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SqlDataReader - ReturnValue - rowcount

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);
SqlCommand _cmd = new SqlCommand("spShowTeamDetails", sc);
_cmd.CommandTimeout = 30;
_cmd.CommandType = CommandType.StoredProcedure;
SqlParameter _paramSocietyID = new SqlParameter("@SocietyID",
SqlDbType.UniqueIdentifier);
_paramSocietyID.Value = SocietyID;
_cmd.Parameters.Add(_paramSocietyID);
SqlParameter _rowCount = new SqlParameter("@ReturnValue", SqlDbType.Int);
_rowCount.Direction = ParameterDirection.ReturnValue;
_cmd.Parameters.Add(_rowCount);

sc.Open();
dr = _cmd.ExecuteReader();

if(Convert.ToInt32(_rowCount.Value) ==1) //_rowCount.Value returns null
even though it returns one record in query analyser
{
while (dr.Read())
{
..........
}
}
else
throw new Exception("Unique society not found.")

In advance - thanks for your help


Nov 8 '06 #1
3 4200
You can get the row count from dr.RecordsAffected property.

- ThazKool

On Nov 8, 8:29 am, "Jon Haakon Ariansen" <j...@spama.nowrote:
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);
SqlCommand _cmd = new SqlCommand("spShowTeamDetails", sc);
_cmd.CommandTimeout = 30;
_cmd.CommandType = CommandType.StoredProcedure;
SqlParameter _paramSocietyID = new SqlParameter("@SocietyID",
SqlDbType.UniqueIdentifier);
_paramSocietyID.Value = SocietyID;
_cmd.Parameters.Add(_paramSocietyID);
SqlParameter _rowCount = new SqlParameter("@ReturnValue", SqlDbType.Int);
_rowCount.Direction = ParameterDirection.ReturnValue;
_cmd.Parameters.Add(_rowCount);

sc.Open();
dr = _cmd.ExecuteReader();

if(Convert.ToInt32(_rowCount.Value) ==1) //_rowCount.Value returns null
even though it returns one record in query analyser
{
while (dr.Read())
{
..........
}}else
throw new Exception("Unique society not found.")

In advance - thanks for your help
Nov 8 '06 #2
"ThazKool" <Ch**********@gmail.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
You can get the row count from dr.RecordsAffected property.
Yes, but only for an action query, not for a SELECT query...
http://msdn.microsoft.com/library/de...classtopic.asp
Nov 8 '06 #3
sql resultsets are returned as a stream that the reader reads. the return
value comes after all resultsets are returned (makes sense if you think
about it. sqlserver will not know the row count until it has read the rows -
which with a firehose are returned as soon as read. the return statement is
after all processing).

after you have read all rows and resultsets you can access the return value.
to process the resultsets eityher loop through them or close the reader
(which will implicitly process them).

-- bruce (sqlwork.com)
"Jon Haakon Ariansen" <jo**@spama.nowrote in message
news:ui**************@TK2MSFTNGP04.phx.gbl...
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);
SqlCommand _cmd = new SqlCommand("spShowTeamDetails", sc);
_cmd.CommandTimeout = 30;
_cmd.CommandType = CommandType.StoredProcedure;
SqlParameter _paramSocietyID = new SqlParameter("@SocietyID",
SqlDbType.UniqueIdentifier);
_paramSocietyID.Value = SocietyID;
_cmd.Parameters.Add(_paramSocietyID);
SqlParameter _rowCount = new SqlParameter("@ReturnValue", SqlDbType.Int);
_rowCount.Direction = ParameterDirection.ReturnValue;
_cmd.Parameters.Add(_rowCount);

sc.Open();
dr = _cmd.ExecuteReader();

if(Convert.ToInt32(_rowCount.Value) ==1) //_rowCount.Value returns null
even though it returns one record in query analyser
{
while (dr.Read())
{
..........
}
}
else
throw new Exception("Unique society not found.")

In advance - thanks for your help


Nov 8 '06 #4

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

Similar topics

5
10994
by: wackyphill | last post by:
Is Set RowCount @RowCount More efficient than simply using TOP? Thanks for any input.
3
10781
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
23473
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
3241
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...
7
29820
by: Rich | last post by:
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?
6
3047
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...
6
6384
by: rn5a | last post by:
Suppose a SQL Server 2005 stored procedure looks like this: ALTER PROCEDURE SPName @UserID int SELECT COUNT(*) FROM Table1 WHERE UserID = @UserID SELECT COUNT(*) FROM Table1 In the ASPX...
3
25935
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
5701
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
7126
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,...
1
6842
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...
0
5434
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,...
1
4865
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...
0
3070
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...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
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 ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
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...

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.