Executescalar, retrieving a value

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! :)
 
D

David Browne

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top