Solved

How to Get Record Count For ExecuteReader();

Posted on 2004-08-29
5
3,773 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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

717 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