Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to Get Record Count For ExecuteReader();

Posted on 2004-08-29
5
Medium Priority
?
4,017 Views
Last Modified: 2008-02-01
Hi,

I know you can get a records affected with ExecuteNonQuery and that you can get a rowcount with ExecuteReader if you are using SqlServer to get an output parameter with the @@ROWCOUNT but I think you can only retreive the @@ROWCOUNTafter closing the connection.  

How then can I get some kind of rowcount before the connection closes?  For example, I want to say, if there is a record then make myVar1=dr[0].ToString(); myVar2=dr[1].ToString(); etc.?  If I can only get the @@ROWCOUNT after closing the connection then I can't really set the variables beforehand because there may not be a record existing.

Where and what do I do to the following?:

SqlDataReader dr=cmd.ExecuteReader();
dr.Read();

//If record exists then set these variables:

myVar1=dr1[0].ToString();
myVar2=dr1[1].ToString();

//Else...do something

myConn.Close();

Thanks
0
Comment
Question by:champ_010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:daffodils
ID: 11927561
You want to check just IF ANY records exist ? or do you want to know HOW MANY records are there ?
0
 
LVL 1

Author Comment

by:champ_010
ID: 11927570

Hmmm...I guess in my current situation it is IF ANY records exists.
0
 
LVL 8

Accepted Solution

by:
daffodils earned 800 total points
ID: 11927580
So,

1. If you want to check just IF ANY records exist ? then you can use SqlDataReader.HasRows Property

if(dr.HasRows)
{
    myVar1=dr1[0].ToString();
    myVar2=dr1[1].ToString();
}
Gets a value indicating whether the SqlDataReader contains one or more rows.
Note --> This namespace, class, or member is supported only in version 1.1 of the .NET Framework.


2. And If you want to know HOW MANY records are there ? you have SqlDataReader.RecordsAffected Property

Gets the number of rows changed, inserted, or deleted by execution of the Transact-SQL statement.

BUT, this property is not set until all rows are read and you close the SqlDataReader.
0
 
LVL 1

Author Comment

by:champ_010
ID: 11927591

THANKS!  An important question answered--will be using it alot!
0
 
LVL 8

Expert Comment

by:daffodils
ID: 11927806
sure.. glad I could help.
This is the MSDN article for SqlDataReader Members.. thought you might like it as a reference.
Best of Luck !

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatasqlclientsqldatareadermemberstopic.asp
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question