입력을 받아들이고 여러 열을 반환하는 저장 프로 시저가 있습니다. 저장 프로 시저는 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
데이터의 행을 반환하지 않습니다 실제로에서 제공
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
내 저장된 proc을 잘못 이해했습니다. 특정 예제에서는 데이터 집합을 반환하지 않으므로 컬렉션 또는 행을 반복 할 때 코드가 더해지지 않고 해당 코드가 투영되어야하며 매개 변수에서 값을 검색해야한다고 말한 것입니다. 다시 한 번 감사드립니다. – BHunt