我有一個接受輸入並返回多個列的存儲過程。存儲過程在我從SSMS執行並且在VS 2013中執行時起作用。但是,當我嘗試使用SqlCommand.ExecuteReader執行它時,讀取器中沒有任何行。如果我從proc和SqlCommand中刪除輸出參數,同時仍然保留一個輸入參數,我可以返回我正在查找的行。SqlCommand.ExecuteReader使用輸出參數使用Stored Proc不返回任何行
這裏是存儲過程
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;
}
不知道。顯然這裏是初學者。我會嘗試。謝謝 – BHunt 2014-10-03 16:01:46
我對存儲過程有一些不正確的理解。在我的特殊示例中,它不會返回數據集,因此迭代通過集合或行不會累加起來,並且應該調整代碼,就像您說我應該從參數中檢索值。再次感謝 – BHunt 2014-10-04 11:29:15