SqlDataReader - ReturnValue - rowcount

  • Thread starter Jon Haakon Ariansen
  • Start date
J

Jon Haakon Ariansen

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
 
B

bruce barker \(sqlwork.com\)

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top