返回SQL Server存储过程结果

时间:2013-02-18 20:09:30

标签: c# .net sql sql-server stored-procedures

我在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

但我似乎无法阅读返回的结果

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;
    }

有什么问题?

由于

5 个答案:

答案 0 :(得分:2)

GetInt32方法将从选定的表输出中读取。您需要返回值,因此您可以将代码更改为

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

答案 1 :(得分:1)

您可以更改存储过程,替换

RETURN @TotalPlaces - @ReservedPlaces;

人:

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

您也可以从存储过程中获取返回值,但这需要进行一些修改。有关详细信息,请参阅this question

答案 2 :(得分:0)

必须使用SqlCommand.Parameters集合中的附加参数读取特定类型的返回值,方向为System.Data.ParameterDirection.ReturnValue

要在尝试时阅读它,您的SQL过程需要执行:

SELECT @TotalPlaces - @ReservedPlaces As MyResult

然后结果将作为结果集而不是返回值返回。

答案 3 :(得分:0)

所以我的存储过程必须像这样?

    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

我的功能是什么?

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;
    }

答案 4 :(得分:0)

在青少年存储过程中尝试此操作。

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

这在你的程序中

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;