Solved

How to Get Record Count For ExecuteReader();

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

808 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