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

标签 c# sql sql-server

我有一个接受输入并返回多列的存储过程。当我从 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;
}

最佳答案

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

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

因此您不需要任何 SqlDataReader 来检索那里的参数。

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

关于c# - SqlCommand.ExecuteReader 使用带输出参数的存储过程不返回任何行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26178032/

相关文章:

c# - LINQ 的字数统计

c# - MongoCollectionSettings.GuidRepresentation 已过时,有什么替代方法?

python - 如何将 pyodbc.row 拆分为其值列表

mysql - 更新 mysql 时出现重复错误

mysql - 上周最受欢迎的帖子

sql-server - 为什么 Hibernate HSQL Concat 不适用于 MSSQL?

sql-server - 一个 SELECT 如何阻止另一个?

c# - 为什么球没有在右边缘反弹

c# - Asp.net 在 sessionState 中加密 sqlConnectionString

sql-server - 跟踪或记录对 SQL Server 中用户定义函数的调用