4 votes

Résultat de la procédure stockée du serveur SQL retourné

J'ai une procédure stockée dans SQL Server :

CREATE PROCEDURE [dbo].[GET_AVAILABLE_PLACES] 
-- Add the parameters for the stored procedure here
@eventGuid uniqueidentifier,
@placeGuid uniqueidentifier,     
@dateGuid dateTime 
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE @ReservedPlaces int;
DECLARE @TotalPlaces int;    
SELECT @ReservedPlaces = RESERVED_PLACES FROM dbo.EVENT_DATE_PLACE
WHERE EVENT_GUID = @eventGuid 
and DATE_BEGIN = @dateGuid
and PLACE_GUID = @placeGuid    
SELECT @TotalPlaces = NUMBER_PLACES FROM dbo.PLACES
WHERE GUID = @placeGuid    
RETURN @TotalPlaces - @ReservedPlaces;     
END

Mais je ne parviens pas à lire le résultat renvoyé.

private int SelectByStoredProcedureGetAvailablePlaces(string entryParam1, string entryParam2, DateTime entryParam3)
{
        int results;
        //PlanningElement plan = GetPlanningElement(entryParam1, entryParam2, entryParam3.ToString(), "31/12/2012 00:00:00", "150");

        using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;

            sqlCommand.CommandText = "GET_AVAILABLE_PLACES";
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue("eventGuid", entryParam1);
            sqlCommand.Parameters.AddWithValue("placeGuid", entryParam2);                
            sqlCommand.Parameters.AddWithValue("dateGuid", entryParam3);

            sqlConnection.Open();

            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            results = sqlDataReader.GetInt32(0);

            sqlConnection.Close();
        }

        return results;
    }

Quel est le problème ?

Merci

2voto

Justin Pihony Points 21088

En GetInt32 va lire la sortie d'une table sélectionnée. Vous voulez une valeur de retour, donc vous pouvez modifier le code comme suit

SqlParameter returnValueParam = sqlcomm.Parameters.Add("@ReturnValue", SqlDbType.Int);
returnValueParam.Direction = ParameterDirection.ReturnValue;
sqlCommand.Parameters.Add(returnValueParam);
...
sqlCommand.ExecuteNonQuery();
result = returnValueParam.Value;

1voto

Jacco Points 2512

Vous pouvez modifier votre procédure stockée, remplacer

RETURN @TotalPlaces - @ReservedPlaces;

Par :

SELECT (@TotalPlaces - @ReservedPlaces) AS [AvailablePlaces]
RETURN;

Vous pouvez également obtenir une valeur de retour de votre procédure stockée, mais cela nécessite quelques modifications supplémentaires. Voir cette question pour plus d'informations.

0voto

maelstrom Points 1111

Ce type particulier de valeur de retour doit être lu en utilisant un paramètre supplémentaire dans la collection SqlCommand.Parameters avec la direction de System.Data.ParameterDirection.ReturnValue.

Pour le lire comme vous l'essayez, votre procédure SQL devrait faire :

SELECT @TotalPlaces - @ReservedPlaces As MyResult

Le résultat sera alors renvoyé sous forme de jeu de résultats au lieu d'une valeur de retour.

0voto

artoon Points 709

Donc ma procédure stockée doit être comme ceci ?

    CREATE PROCEDURE [dbo].[GET_AVAILABLE_PLACES] 
-- Add the parameters for the stored procedure here
@eventGuid uniqueidentifier,
@placeGuid uniqueidentifier,     
@dateGuid dateTime 
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE @ReservedPlaces int;
DECLARE @TotalPlaces int;    
SELECT @ReservedPlaces = RESERVED_PLACES FROM dbo.EVENT_DATE_PLACE
WHERE EVENT_GUID = @eventGuid 
and DATE_BEGIN = @dateGuid
and PLACE_GUID = @placeGuid    
SELECT @TotalPlaces = NUMBER_PLACES FROM dbo.PLACES
WHERE GUID = @placeGuid    
SELECT @TotalPlaces - @ReservedPlaces As ReturnValue;   
END

Et ma fonction est la suivante ?

private int SelectByStoredProcedureGetAvailablePlaces(string entryParam1, string entryParam2, DateTime entryParam3)
{
        int results;
        //PlanningElement plan = GetPlanningElement(entryParam1, entryParam2, entryParam3.ToString(), "31/12/2012 00:00:00", "150");

        using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;

            sqlCommand.CommandText = "GET_AVAILABLE_PLACES";
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue("eventGuid", entryParam1);
            sqlCommand.Parameters.AddWithValue("placeGuid", entryParam2);                
            sqlCommand.Parameters.AddWithValue("dateGuid", entryParam3);

            SqlParameter returnValueParam = sqlCommand.Parameters.Add("@ReturnValue", SqlDbType.Int);
            returnValueParam.Direction = ParameterDirection.ReturnValue;
            sqlCommand.Parameters.Add(returnValueParam);

            sqlConnection.Open();

            sqlCommand.ExecuteNonQuery();
            result = returnValueParam.Value;

            sqlConnection.Close();
        }

        return results;
    }

0voto

Librit Points 1

Essayez ceci dans votre quatrième procédure d'échantillonnage.

CREATE PROCEDURE [dbo].[GET_AVAILABLE_PLACES] 
-- Add the parameters for the stored procedure here
@eventGuid uniqueidentifier,
@placeGuid uniqueidentifier,     
@dateGuid dateTime,

@ReservedPlaces int output,
@TotalPlaces int output   

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here

SELECT @ReservedPlaces = RESERVED_PLACES FROM dbo.EVENT_DATE_PLACE
WHERE EVENT_GUID = @eventGuid 
and DATE_BEGIN = @dateGuid
and PLACE_GUID = @placeGuid    
SELECT @TotalPlaces = NUMBER_PLACES FROM dbo.PLACES
WHERE GUID = @placeGuid    
SELECT @TotalPlaces - @ReservedPlaces As ReturnValue;   
END

Et ceci dans votre programme

private int SelectByStoredProcedureGetAvailablePlaces(string entryParam1, string   entryParam2, DateTime entryParam3)
{
        int results;
        //PlanningElement plan = GetPlanningElement(entryParam1, entryParam2, entryParam3.ToString(), "31/12/2012 00:00:00", "150");

        using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;

            sqlCommand.CommandText = "GET_AVAILABLE_PLACES";
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue("eventGuid", entryParam1);
            sqlCommand.Parameters.AddWithValue("placeGuid", entryParam2);                
            sqlCommand.Parameters.AddWithValue("dateGuid", entryParam3);

            SqlParameter returnValueParam = sqlCommand.Parameters.Add("@ReturnValue", SqlDbType.Int);
            returnValueParam.Direction = ParameterDirection.Output;

            sqlConnection.Open();

            sqlCommand.ExecuteNonQuery();
            result = returnValueParam.Value;

            sqlConnection.Close();
        }

        return results;

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X