Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / February 2007

Tip: Looking for answers? Try searching our database.

Data Reader

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Helen Trim - 31 Jan 2007 09:20 GMT
Is there any way to know how many records a data reader returns, without
having to read each one and increment a counter?

I am creating an ASP.NET 2.0 project in C#.

I need to read data from the first record, if there is only one and count
them if there is more than one.  The code that I have written looks like this:

dr = cd.ExecuteReader();
if (dr.Read())
{
        intPatientCount=1;
        strPatNo = Convert.ToString(dr["pat_no"]);  // Store the pat_no in
case there is only one
        while (dr.Read())
        {
               intPatientCount++;
         }
}
dr.Close();

It works, but it's not very elegant or efficient.  Can anyone suggest a
better way?

Thanks,
Helen
Ben Voigt - 07 Feb 2007 14:51 GMT
> Is there any way to know how many records a data reader returns, without
> having to read each one and increment a counter?
[quoted text clipped - 4 lines]
> them if there is more than one.  The code that I have written looks like
> this:

In general, I don't know of a better way.  However, if you are dealing with
database access, it may be far more efficient to run a "SELECT COUNT(*)
WHERE...." query first to get the count, then get the actual data only if
the count is 1.

> dr = cd.ExecuteReader();
> if (dr.Read())
[quoted text clipped - 14 lines]
> Thanks,
> Helen

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.