Executescalar, retrieving a value

  • Thread starter Thread starter Steve Schroeder
  • Start date Start date
S

Steve Schroeder

From MSDN:
http://msdn.microsoft.com/library/d...stemDataIDbCommandClassExecuteScalarTopic.asp

Use the ExecuteScalar method to retrieve a single value (for example, an
aggregate value) from a database.

Public Sub CreateMySqlCommand(myScalarQuery As String, myConnection As
SqlConnection)
Dim myCommand As New SqlCommand(myScalarQuery, myConnection)
myCommand.Connection.Open()
myCommand.ExecuteScalar()
myConnection.Close()
End Sub 'CreateMySqlCommand

....

Thanks Billy...ExecuteScalar is used to retrieve a single value, and then
your example doesn't even reflect retrieving a value...hello! Anyone home?

Ok, sarcasm aside, could someone expound on this flawed example that shows
how a value returned from a stored procedure could be assigned to a variable
expression?

Thank you! :)
 
Steve Schroeder said:
From MSDN:
http://msdn.microsoft.com/library/d...stemDataIDbCommandClassExecuteScalarTopic.asp

Use the ExecuteScalar method to retrieve a single value (for example, an
aggregate value) from a database.

Public Sub CreateMySqlCommand(myScalarQuery As String, myConnection As
SqlConnection)
Dim myCommand As New SqlCommand(myScalarQuery, myConnection)
myCommand.Connection.Open()
myCommand.ExecuteScalar()
myConnection.Close()
End Sub 'CreateMySqlCommand

...

Thanks Billy...ExecuteScalar is used to retrieve a single value, and then
your example doesn't even reflect retrieving a value...hello! Anyone home?

Ok, sarcasm aside, could someone expound on this flawed example that shows
how a value returned from a stored procedure could be assigned to a
variable
expression?

ExecuteScalar is not for stored procedure. It simply returns the first
column of the first row as a scalar value. I wish it had never been
invented.

To return a value from a stored procedure, bind a SqlParameter to the
command with ParameterDirection.Output.

David
 
Back
Top