• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2374
  • Last Modified:

How do you get rowcount with SqlDataReader?

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
Anders-Leet
Asked:
Anders-Leet
  • 2
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
Anders-LeetAuthor Commented:
Ok good.  I have what I need, now how do I properly close the DataSet/SqlDataAdaptor objects?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
the dataadapter and dataset just needs to be set to null.
0
 
Anders-LeetAuthor Commented:
Ok thank you both.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now