Solved

How do you get rowcount with SqlDataReader?

Posted on 2008-09-30
4
2,345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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