简体   繁体   English

返回SQL Server存储过程结果

[英]SQL Server stored procedure result returned

I have a stored procedure in SQL Server : 我在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

But I can not seem to read the result returned 但我似乎无法读取返回的结果

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

What is the problem ? 问题是什么 ?

Thanks 谢谢

The GetInt32 method will read from a selected table output. GetInt32方法将从选定的表输出中读取。 You want a return value, so you can change the code to be 您想要一个返回值,因此您可以更改代码

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

You can alter your stored procedure, replace 您可以更改存储过程,替换

RETURN @TotalPlaces - @ReservedPlaces;

By: 通过:

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

Your can also get a return value from your stored procedure, but that requires some more modifications. 您也可以从存储过程中获取返回值,但这需要进行一些更改。 See this question for more information. 有关更多信息,请参阅此问题

That particular type of return value must be read by using an additional parameter in the SqlCommand.Parameters collection with direction of System.Data.ParameterDirection.ReturnValue 必须通过使用SqlCommand.Parameters集合中的附加参数来读取该特定类型的返回值,其方向为System.Data.ParameterDirection.ReturnValue

To read it as you are attempting, your SQL procedure would need to do: 要在尝试时阅读它,您的SQL过程需要执行以下操作:

SELECT @TotalPlaces - @ReservedPlaces As MyResult

Then the result will be returned as a resultset instead of a return value. 然后结果将作为结果集而不是返回值返回。

So my stored procedure must be as this ? 所以我的存储过程必须如此?

    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

And my function as this ? 而我的功能呢?

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

Try this in yourth strored procedure. 在yourth strored程序中尝试这个。

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

And this in your programm 这在你的程序中

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM