【问题标题】:SQL Server stored procedure result returnedSQL Server 存储过程结果返回
【发布时间】:2013-02-18 20:09:30
【问题描述】:

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

有什么问题?

谢谢

【问题讨论】:

  • 阅读器读取行,而不是返回代码。如果您将 RETURN @TotalPlaces - @ReservedPlaces; 替换为 SELECT @TotalPlaces - @ReservedPlaces;,您现有的代码将起作用

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你可以改变你的存储过程,替换

    RETURN @TotalPlaces - @ReservedPlaces;
    

    作者:

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

    您也可以从存储过程中获取返回值,但这需要进行更多修改。请参阅this question 了解更多信息。

    【讨论】:

      【解决方案3】:

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

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

      SELECT @TotalPlaces - @ReservedPlaces As MyResult
      

      那么结果将作为结果集而不是返回值返回。

      【讨论】:

        【解决方案4】:

        所以我的存储过程一定是这样的?

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

        【讨论】:

          【解决方案5】:

          在你的程序中试试这个。

          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-12-07
            • 1970-01-01
            • 2016-12-17
            • 1970-01-01
            • 1970-01-01
            • 2010-11-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多