';指定的强制转换无效';调用SQL Server存储过程时出错

本文关键字:SQL Server 出错 调用 存储过程 转换 无效 | 更新日期: 2023-09-27 18:27:23

我正在用以下代码调用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

';指定的强制转换无效';调用SQL Server存储过程时出错

您的问题源于您的一个强制转换失败,很可能是模型中的一个可为null的类型。对于可为null的类型,从强制转换更改为使用as,这将导致DbNull.Value(即读取器返回的内容)成为所需的null值。

    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"];