473,378 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Record count in SqlDataReader

Hi:

When returning an SqlDataReader from a database query, how do you get a
count of the number of records contained in the reader? There is no "Count"
property for reader object.

Thanks,
Charlie
Dec 21 '05 #1
2 3614
A reader doesn't have a count. You will need to read all the records and
keep track of count yourself.

Better to use a Command.ExecuteScalar() or to use a dataAdaptor and load
your records into a DataSet or Table and get your count there.

using (SqlConnection cn = new SqlConnection(DataConnection))
using (SqlCommand cm = new SqlCommand("SELECT Count(*) FROM MyTable", cn))

{

cn.Open();

cm.CommandType = CommandType.Text;

int i = (int)cm.ExecuteScalar();

cn.Close();

return i;

}
--

Andrew Robinson
www.binaryocean.com
www.bellinghamdotnet.org
"Charlie@CBFC" <ch*****@comcast.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi:

When returning an SqlDataReader from a database query, how do you get a
count of the number of records contained in the reader? There is no
"Count"
property for reader object.

Thanks,
Charlie

Dec 21 '05 #2
There is no count because the reader is streaming the results in form the
DB server. IOW, it's possible the DB server is still finding records that
match your criteria after you start using the data reader.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi:

When returning an SqlDataReader from a database query, how do you get
a count of the number of records contained in the reader? There is no
"Count" property for reader object.

Thanks,
Charlie

Dec 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: 6thirty | last post by:
Hi, I've created a stocktaking database using Access XP. This is indexed by two fields - part number and shelf location. I am currently inputting all the data via a form. When I have entered a...
1
by: Tom | last post by:
Hi How to read the LAST record of datareader? Here is my code ) this.EndDate = Convert.ToDateTime(dr) [/code
1
by: darrel | last post by:
I'm trying to whip up a fancy repeater control that will put records into a two-column table for me. This is how I envision it working: itemtemplate if record count = odd then write out the...
0
by: Sunil Sabir | last post by:
Dear All, I have a web form which has a link and a drop down box. when I press a link a pop up window opens. This window has a lot of TEXTBOXES and a Save button. When I fill the TextBoxes and...
6
by: Opie | last post by:
What would be a more efficient way for me to determine if a record in an SQL DB table exists? Right now, I have a try/catch like this: try {...
4
by: Jason Kumpf | last post by:
OK I've been staring at this code all day and still with everything I have tried I cannot figure out two problems I am having. Once is why the space limit for the directory I create in the code...
6
by: zoro | last post by:
Hi, I am looking for the recommended way to retrieve several values from a single record, i.e. a typical lookup scenario. An example would be a query that needs to retrieve user_name, user_addres,...
1
by: Tin Gherdanarra | last post by:
I understand that an SqlDataReader instance acts as a cursor to a set of query results. This works fine: while(readerInstance.Read()) { Console.WriteLine("{0} {1} {2}", readerInstance,...
2
by: rn5a | last post by:
A SQL Server 2005 stored procedure expects a parameter 'UserID' depending upon which it retrieves the no. of records & OrderIDs corresponding to the 'UserID' from a DB table (note that OrderID &...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.