Monday, February 20, 2012

RecordCount with Datareader

I am very disappinted where Datareader have no RecordCount where I can get the total records it read. I guess I found a way:

sql = "SELECT *, ROW_NUMBER() OVER (ORDER BY id DESC) AS c FROM ACCOUNTS ORDER BY c DESC"
Dim command As SqlCommand = New SqlCommand(sql, New SqlConnection(_DBConnectionString))
command.Connection.Open()
_ReturnDataReader = command.ExecuteReader(CommandBehavior.CloseConnection)
command.Dispose()
_TotalRecord = _ReturnDataReader.GetInt64(_ReturnDataReader.GetOrdinal("c"))

Have SQL Server 2005 count for me...

Hi,

You can get the record count this way:

Dim

drAs SqlDataReader = AnyFunctionThatReturnsSqlDataReader()
Dim reccountAsInteger = 0
While dr.Read
reccount += 1
EndWhile

Good luck!

Wilmar, KSA (Mabuhay!)

No comments:

Post a Comment