MiloszSkalecki
Wed Aug 31 10:47:20 CDT 2005
Hi,
Yes it can be used with stored procedures because stored procs can return
result set.
private int Test()
{
SqlConnection oConnection = new SqlConnection(cszConnectionString);
SqlCommand oCommand = new SqlCommand("MyStoredProcedure", oConnection);
oCommand.CommandType = CommandType.StoredProcedure;
try
{
oConnection.Open();
return (int) oCommand.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (oConnection.State != ConnectionState.Closed)
oConnection.Close();
}
// test stored proc
CREATE PROCEDURE [MyStoredProcedure] AS
SELECT 1
GO
--
Milosz Skalecki
MCP, MCAD
"David Browne" wrote:
>
> "Steve Schroeder" <sschroeder@somewhere.com> wrote in message
> news:e%23z6t0jrFHA.1984@tk2msftngp13.phx.gbl...
> > From MSDN:
> >
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataIDbCommandClassExecuteScalarTopic.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
>
>
>