我使用以下代码调用SQL Server数据库中的存储过程。
我们的想法是将相关事件添加到EventSchedule
模型属性的列表中,但代码返回
指定的演员表无效
错误。存储过程从我构建的视图中提取数据。调用堆栈显示错误在第96行,不确定是否会帮助任何人。我想我可能需要另一双眼睛才能看到我在这里失踪的东西。
var command = new SqlCommand("GetEvents", conn)
{
CommandType = CommandType.StoredProcedure
};
command.Parameters.Add(new SqlParameter("Id", SqlDbType.BigInt, 0, "id"));
command.Parameters[0].Value = schedule.LeagueId;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
//line 96
var _event = new Event
{
EventId = (Int64) reader["eventid"],
HomeTeamId = (Int64) reader["home_team_id"],
TeamName = (string) reader["team_name"],
HomeTeamScore = (int) reader["home_team_score"],
AwayTeamId = (Int64) reader["away_team_id"],
AwayTeamName = (string) reader["Expr1"],
AwayTeamScore = (int) reader["away_team_score"],
WinningTeamId = (Int64) reader["winning_teamid"],
EventStartDttm = (DateTime) reader["event_start_dttm"],
CurrentDttm = (DateTime) reader["current_dttm"],
Locked = (bool) reader["locked"]
};
schedule.Events.Add(_event);
}
}
return schedule;
型号:
public class EventSchedule
{
public Int64 UserId { get; set; }
public Int64 LeagueId { get; set; }
public string League { get; set; }
public int Season { get; set; }
public int Week { get; set; }
public IList<Event> Events { get; set; }
}
public class Event
{
public Int64 EventId { get; set; }
public Int64 HomeTeamId { get; set; }
public string TeamName { get; set; }
public int? HomeTeamScore { get; set; }
public Int64 AwayTeamId { get; set; }
public string AwayTeamName { get; set; }
public int? AwayTeamScore { get; set; }
public Int64 WinningTeamId { get; set; }
public DateTime EventStartDttm { get; set; }
public DateTime CurrentDttm { get; set; }
public bool Locked { get; set; }
}
存储过程:
ALTER PROCEDURE [dbo].[GetEvents]
@Id int
AS
BEGIN
SET NOCOUNT ON;
SELECT
eventid,
home_team_id,
team_name,
home_team_score,
away_team_id,
Expr1,
away_team_score,
winning_teamid,
event_start_dttm,
current_dttm,
locked
FROM
leagueScheduleForCurrentWeek
WHERE
id = @Id
END
答案 0 :(得分:2)
你的问题来自于你的一个演员表失败,很可能是你的模型中的一个可空类型。对于可为空的类型,从强制类型转换为使用as
,这将导致DbNull.Value
(这是读者返回的内容)为您想要的空值。
var _event = new Event
{
EventId = (Int64) reader["eventid"],
HomeTeamId = (Int64) reader["home_team_id"],
TeamName = (string) reader["team_name"],
HomeTeamScore = reader["home_team_score"] as int?, //here
AwayTeamId = (Int64) reader["away_team_id"],
AwayTeamName = (string) reader["Expr1"],
AwayTeamScore = reader["away_team_score"] as int?, //and here
WinningTeamId = (Int64) reader["winning_teamid"],
EventStartDttm = (DateTime) reader["event_start_dttm"],
CurrentDttm = (DateTime) reader["current_dttm"],
Locked = (bool) reader["locked"]
};
如果您的问题仍然存在,那么您的模型与您的数据表不匹配。你需要通过你的类型来找出哪一个不匹配。你可以通过从构造函数中删除赋值来使自己更容易。
var _event = new Event();
_event.EventId = (Int64) reader["eventid"];
_event.HomeTeamId = (Int64) reader["home_team_id"];
_event.TeamName = (string) reader["team_name"];
_event.HomeTeamScore = reader["home_team_score"] as int?;
_event.AwayTeamId = (Int64) reader["away_team_id"];
_event.AwayTeamName = (string) reader["Expr1"];
_event.AwayTeamScore = reader["away_team_score"] as int?;
_event.WinningTeamId = (Int64) reader["winning_teamid"];
_event.EventStartDttm = (DateTime) reader["event_start_dttm"];
_event.CurrentDttm = (DateTime) reader["current_dttm"];
_event.Locked = (bool) reader["locked"];