DISABLE ADBLOCK

ADBlock is blocking some content on the site

ADBlock errore

Ado.Net - Return true or false from ExecuteScalar and ExecuteNonQuery

StackOverflow https://stackoverflow.com/questions/13216745

Question

I'm new to dotnet, can anyone please help.

What we require is to return status true or false when we run ExecuteScalar or ExecuteNonquery ado commands in our vb.net code.

Regards

Solution

Simply write an extension method for SQLCommand wrapping the original functionality.

<Extension()> 
Public Function MyExecuteScalar(ByVal sqlCommand As SqlCommand) As Boolean
    MyExecuteScalar = Not (sqlCommand.ExecuteScalar() is Nothing)
End Function

<Extension()> 
Public Function MyExecuteNonQuery(ByVal sqlCommand As SqlCommand) As Boolean
    MyExecuteNonQuery = sqlCommand.ExecuteNonQuery() > 0
End Function

Call these methods simply like this:

if mySqlCommand.MyExecutescalar() then ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow