Solved

How to Get Record Count For ExecuteReader();

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now