executescalar()

  • Thread starter Thread starter N! Xau
  • Start date Start date
N

N! Xau

Hi,

I'm trying to get the row count from a table, in the fastest way (con
is the connection name).

Dim NRows As Integer
Dim CountQuery As String = "select count(*) from MyTable"
Dim cmdNR As New OracleCommand(CountQuery, con)
Try
NRows = cmdNR.ExecuteScalar()
Return NRows
Catch ex As Exception
....
End Try


I get an Exception.

TIA
N! Xau
 
In addition to what the others have said, from the looks of it you do not
have Option Strict On. You should always have it on (not that it necessarily
relates the problem at hand).
 
ExecuteScalar also returns an object. It's a good idea to explicitly cast
the result to the type you're looking. It's also a good idea to check for a
DBNull.Value or Nothing before trying to assign the result to a variable.
All this can be done in one function.

Public Function SafeInt(ByVal o As Object) As Integer
Dim i As Integer
If (o Is Nothing OrElse o Is DBNull.Value) Then
i = 0
Else
i = CType(o, Integer)
End If
Return (i)
End Function
 
Back
Top