• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2371
  • 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.
SELECT     f_title
FROM         features
WHERE     (pid = 1)
Dim oCom As SqlCommand = New SqlCommand(sqlStuff.featureList(theId), New SqlConnection(sqlStuff.csString))
Dim oRead As SqlDataReader
    oRead = oCom.ExecuteReader
    Dim temp As Integer = oRead.RecordsAffected 'returns -1?
Catch ex As Exception
End Try

Open in new window

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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