Solved

How do you get rowcount with SqlDataReader?

Posted on 2008-09-30
4
2,277 Views
Last Modified: 2012-06-22
I have a routine that selects data from a SQL database, and returns it using a SqlDataReader.  See my snippet for the code I am using.  I am unsure of the best way to get the rowcount of the SQL I executed.   Executing the query inside SQL Server Management Studio yields 7 rows.
// QUERY

SELECT     f_title

FROM         features

WHERE     (pid = 1)
 

//CODE

Dim oCom As SqlCommand = New SqlCommand(sqlStuff.featureList(theId), New SqlConnection(sqlStuff.csString))

Dim oRead As SqlDataReader

Try

    oCom.Connection.Open()

    oRead = oCom.ExecuteReader

    oRead.Read()

    Dim temp As Integer = oRead.RecordsAffected 'returns -1?

    Stop

Catch ex As Exception
 

End Try

Open in new window

0
Comment
Question by:Anders-Leet
  • 2
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
if you REALLY need the row count, fill a DataSet instead of using a Reader. from there, the datatable's Rows.Count will give the correct value.
0
 

Author Comment

by:Anders-Leet
Comment Utility
Ok good.  I have what I need, now how do I properly close the DataSet/SqlDataAdaptor objects?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
the dataadapter and dataset just needs to be set to null.
0
 

Author Comment

by:Anders-Leet
Comment Utility
Ok thank you both.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

16 Experts available now in Live!

Get 1:1 Help Now