4

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

问题是什么 ?

谢谢

4

5 回答 5

2

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

SqlParameter returnValueParam = sqlcomm.Parameters.Add("@ReturnValue", SqlDbType.Int);
returnValueParam.Direction = ParameterDirection.ReturnValue;
sqlCommand.Parameters.Add(returnValueParam);
...
sqlCommand.ExecuteNonQuery();
result = returnValueParam.Value;
于 2013-02-18T20:19:29.853 回答
1

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

RETURN @TotalPlaces - @ReservedPlaces;

经过:

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

您还可以从存储过程中获取返回值,但这需要进行更多修改。有关更多信息,请参阅此问题

于 2013-02-18T20:13:48.823 回答
0

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

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

SELECT @TotalPlaces - @ReservedPlaces As MyResult

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

于 2013-02-18T20:13:16.007 回答
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;
于 2014-06-14T15:21:21.090 回答
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;
    }
于 2013-02-19T11:59:53.963 回答