Record Count in ADO.NET

  • Thread starter Thread starter Darren Gulliver
  • Start date Start date
D

Darren Gulliver

Hi,

When you run a select query how do you return the number of records in the record set?

IE:

Try
Dim objCommand As New SqlClient.SqlCommand ("Select Field1, Field2 on from Table", objConn)
Dim objReader As SqlClient.SqlDataReader

DBConnection.OpenConnection()

objReader = objCommand.ExecuteReader
iRecordsRtn = objReader.?
'''###Long time since I used ADO but I'm sure you could use rs.RecordCount
Catch err As Exception
display error
End Try

TIA
Darren
 
Daren,

Dim cmd As SqlCommand
cmd = New SqlCommand("select count(*) from tbl", conn)
Dim count As Integer = CInt(cmd.ExecuteScalar())

I hope this helps?

Cor
 
A DataReader is a forward only cursor. You have to get to the end before you
can get a record count. With a DataSet, you can get a record count without
having to curse through every record.

Even in ADO, you had to either use a cursor other than default (and
movelast, movefirst) or get to the end for record counts.

---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
A data reader has no concept of number of rows, it is considered to be a
forward only raw firehose.

Dim cnt as Integer = 0
do while objReader.Read()
cnt +=1 ' increment your own counter
' do stuff with the row
Console.WriteLine("{0}, {1)", objReader.GetString("Field1"),
objReader.GetString("Field2"))
loop
Console.WriteLine("Record count: {0}", cnt)
objReader.Close() ' Be sure to close all Readers!

If instead you were to fill a Dataset, then ds.tables("Table").rows.count
would have the value you are looking for.

Hi,

When you run a select query how do you return the number of records in the
record set?

IE:

Try
Dim objCommand As New SqlClient.SqlCommand ("Select Field1, Field2 on
from Table", objConn)
Dim objReader As SqlClient.SqlDataReader

DBConnection.OpenConnection()

objReader = objCommand.ExecuteReader
iRecordsRtn = objReader.?
'''###Long time since I used ADO but I'm sure you could use rs.RecordCount
Catch err As Exception
display error
End Try

TIA
Darren
 
Back
Top