简体   繁体   English

调用SQL Server存储过程时出现“指定的转换无效”错误

[英]'Specified cast is not valid' error on call to SQL Server stored procedure

I'm calling a stored procedure in a SQL Server database with the following code. 我正在使用以下代码在SQL Server数据库中调用存储过程。

The idea is to add the relevant events to a list that is a property of the EventSchedule model, but the code is returning a 这个想法是将相关事件添加到EventSchedule模型的属性列表中,但是代码返回一个

Specified cast is not valid 指定的演员表无效

error. 错误。 The stored procedure pulls data from a view I've built. 存储过程从我建立的视图中提取数据。 The call stack shows the error is at line 96, not sure if that'll help anyone much. 调用堆栈显示错误在第96行,不确定是否会对任何人有很大帮助。 I think I may need another set of eyes to see what I'm missing here. 我想我可能需要另一双眼睛才能看到我在这里缺少的东西。

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;

Models: 楷模:

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; }
}

Stored proc: 存储过程:

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

Your problem comes from the fact that one of your casts are failing, most likely one of the nullable types from your model. 您的问题来自以下事实:您的一种强制转换失败,很可能是模型中的可空类型之一。 Change from a cast to using as for the nullable types, this will cause DbNull.Value (which is what is being returned by your reader) to be come the null value you want. 从铸造到使用更改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"]
    };

If your problem still exists then your model does not match your data table. 如果问题仍然存在,则您的模型与数据表不匹配。 you will need to go through your types to find out which one does not match. 您将需要仔细检查您的类型,以找出不匹配的类型。 You can make this easier on yourself by breaking out the assignment out from the constructor. 您可以通过从构造函数中分配分配来简化此工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM