I have for example this stored procedure
ALTER PROCEDURE dbo.PersonaDoc
@cDocumento varchar (20)
AS
SELECT Ruta_Line
FROM Persona
WHERE (documento= @cDocumento)
What difference would it make to consume it in C# using ExecuteNonQuery, ExecuteScalar or ExecuteReader (depending on the case the values you want to return) if the stored procedure is
ALTER PROCEDURE dbo.PersonaDoc
@cDocumento varchar (20)
@resultado int OUTPUT
AS
SELECT SET@resultado = nombre
FROM Persona
WHERE (documento= @cDocumento)
What is not clear to me is the difference between consuming the data that the stored procedure brings directly or using an output value for it, I suppose that in the first case the advantage would be that I can bring several values and with the OUTPUT only one, but assuming i just want a return value is it the same?
Differences:
ExecuteNonQuery
will execute the Stored Procedure and will not return any resultsExecuteScalar
will return a single data, with which you can make use of the side of your applicationExecuteReader
is used when you want to get a set of data i.e. multiple rows and multiple columns.Now, taking as reference the Stored Procedure
When executing:
ExecuteNonQuery
you will not get resultExecuteScalar
it will turn younull
around since in it youSELECT
make an assignment with@resultado = nombre
, to return a result you must addSELECT @resultado
to the end of your Stored ProcedureExecuteReader
in the same way it will return younull
, you must do the same as theExecuteScalar
or in its case aSELECT * FROM Persona
(as an example)Update
With
OUTPUT
it you make a reference by value to the variable that you have assigned as output value, in the case of your Stored Procedure you could useExecuteScalar
orOUTPUT
, both can be adjusted to the way you program. If you are using C#, a code example would be the following to get the value of the document usingDirection = ParameterDirection.Output;
: