Solved

How do you get rowcount with SqlDataReader?

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

Suggested Solutions

Title # Comments Views Activity
Winform Module - What is the ASP.Net equiv 2 38
Help with opening combobox using VB.NET. 3 34
Help with error in Query 2 38
vb.net deleting excel sheet in workbook 11 30
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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

713 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