SqlCommand.ExecuteReader使用带输出参数的存储过程不返回任何行

BHunt

我有一个接受输入并返回多列的存储过程。当我从SSMS和VS 2013内部执行存储过程时,该存储过程可以工作。但是,当我尝试使用SqlCommand.ExecuteReader执行该存储过程时,阅读器中没有任何行。如果我从proc和SqlCommand中删除输出参数,同时仍保留一个输入参数,则可以返回要查找的行。

这是存储的过程

create Proc sp_ReturnSingleGame
    @GameName varchar(100) output,
    @PlatformName varchar(50) output,
    @ConditionShortDescription varchar(30) output,
    @RetailCost decimal(6,2) output,
    @InStock bit output,
    @GameID int
AS
    select @GameName = GameName, @PlatformName = PlatformName, 
           @ConditionShortDescription = ConditionShortDescription, @RetailCost = RetailCost
         from   Games inner join Condition 
         on     Games.ConditionID = Condition.ConditionID 
                inner join ConsolePlatform
         on Games.PlatformID = ConsolePlatform.PlatformID
         Where Games.GameID = @GameID

    if exists (select GameID 
               From SaleItemized
               Where GameID = @GameID)
        Begin
            set @InStock = 1;
        end
    else
        Begin
            set @InStock = 0;
        end

这是我的C#代码

public Game ReturnSingleGame(int gameId)
{
    SqlConnection connection = new SqlConnection(@"server=mylaptop; integrated security=true; database=GameStoreDB;");

    SqlCommand command = this.ReturnCommandForSp_ReturnSingleGame(connection, gameId);

    try
    {
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        if (reader.HasRows == true)
        {
            reader.Read();
            game.GameId = gameId;
            game.GameName = reader["GameName"].ToString();
            game.PlatformName = reader["PlatformName"].ToString();
            game.RetailCost = (decimal) reader["RetailCost"];
        }
        else
        {
            var exception = new ApplicationException("Game was not found");
            throw exception;
        }
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        connection.Close();
    }
    return game;
}

private SqlCommand CommandForSp_ReturnSingleGame(SqlConnection connection, int gameId)
{
    string storedProc = @"dbo.sp_ReturnSingleGame";

    SqlCommand command = new SqlCommand(storedProc, connection);
    command.CommandType = CommandType.StoredProcedure;

    command.Parameters.Add("@GameName", SqlDbType.VarChar, 100, "GameName");
    command.Parameters["@GameName"].Direction = ParameterDirection.Output;

    command.Parameters.Add("@PlatformName", SqlDbType.VarChar, 50, "PlatformName");
    command.Parameters["@PlatformName"].Direction = ParameterDirection.Output;

    command.Parameters.Add("@ConditionShortDescription", SqlDbType.VarChar, 30, "ConditionShortDescription");
    command.Parameters["@ConditionShortDescription"].Direction = ParameterDirection.Output;

    command.Parameters.Add("@RetailCost", SqlDbType.Decimal);
    command.Parameters["@RetailCost"].SourceColumn = "RetailCost";
    command.Parameters["@RetailCost"].Precision = 6;
    command.Parameters["@RetailCost"].Scale = 2;
    command.Parameters["@RetailCost"].Direction = ParameterDirection.Output;

    command.Parameters.Add("@InStock", SqlDbType.Bit);
    command.Parameters["@InStock"].SourceColumn = "InStock";
    command.Parameters["@InStock"].Direction = ParameterDirection.Output;

    command.Parameters.Add("@GameID", SqlDbType.Int).Value = gameId;
    command.Parameters["@GameID"].SourceColumn = "GameID";
    command.Parameters["@GameID"].Direction = ParameterDirection.Input;

    command.Prepare();

    return command;
}
安迪·科涅涅夫(Andy Korneyev)

您提供的存储过程实际上不返回任何数据行。

它所做的一切-只是设置输出参数。

因此,您无需SqlDataReader检索任何参数。

只需调用command.ExecuteNonQuery(),然后从中获取参数值command.Parameters["@GameName"].Value,依此类推。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

SqlCommand.ExecuteReader使用带输出参数的存储过程不返回任何行

来自分类Dev

带参数的SqlCommand

来自分类Dev

SqlCommand参数Add与AddWithValue

来自分类Dev

SqlCommand参数Add与AddWithValue

来自分类Dev

选择SqlCommand返回字符

来自分类Dev

使用SqlCommand和范围

来自分类Dev

从SqlCommand获取输出参数返回null

来自分类Dev

SqlCommand.ExecuteReader() 如何工作?

来自分类Dev

SqlCommand参数与String.Format

来自分类Dev

C# SqlCommand 返回 0

来自分类Dev

使用SQLCommand / Connection时使用

来自分类Dev

使用SUM()的SqlDataReader和SqlCommand

来自分类Dev

带参数的SQLCommand不再更新我的表

来自分类Dev

SqlCommand.ExecuteReader变得非常慢

来自分类Dev

SqlCommand只返回一行

来自分类Dev

将参数正确传递给SqlCommand

来自分类Dev

SqlCommand如何处理整数类型的参数?

来自分类Dev

集合中未包含SqlCommand参数

来自分类Dev

获取sqlcommand的参数以字符串

来自分类Dev

C#SqlCommand和非命名参数

来自分类Dev

SqlCommand-SqlConnection使用处置问题

来自分类Dev

使用C#SqlCommand的动态SQL

来自分类Dev

创建一个使用SqlCommand的类

来自分类Dev

没有使用参数化SqlCommand的行

来自分类Dev

SQLCommand.CreateParameter.Value = DBNull.Value不返回结果

来自分类Dev

使用sqlcommand在asp.net上检索更新的参数值

来自分类Dev

尝试使用C#执行查询时SqlCommand返回错误

来自分类Dev

使用SqlCommand执行存储过程时,如何防止出现超时错误?

来自分类Dev

使用 EXECUTE AS 语句和 .NET SqlCommand 运行 t-sql 存储过程