[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do you get rowcount with SqlDataReader?

Posted on 2008-09-30
4
Medium Priority
?
2,366 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 22608249
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
ID: 22608885
Ok good.  I have what I need, now how do I properly close the DataSet/SqlDataAdaptor objects?
0
 
LVL 143

Expert Comment

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

Author Comment

by:Anders-Leet
ID: 22609050
Ok thank you both.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline

830 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