Solved

How do you get rowcount with SqlDataReader?

Posted on 2008-09-30
4
2,294 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
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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

20 Experts available now in Live!

Get 1:1 Help Now