【问题标题】:Geting an error when executin the stored Procedure in my MVC3?在我的 MVC3 中执行存储过程时出错?
【发布时间】:2012-09-04 05:15:28
【问题描述】:

大家好,我有一个方法,我将一些参数传递给存储过程,当我执行它时,我得到了这个错误

[从具体化的“System.Int32”类型到“System.String”类型的指定转换无效。]

这就是我调用存储过程的方式

  public JsonResult CreatedBugs()
    {
        int year;
        int month;
        int projectid;
        year = 2012;
        month = 8;
        projectid = 16;
        var loggedbugs = db.ExecuteStoreQuery<LoggedBugs>("LoggedBugs @Year,@Month,@ProjectID", new SqlParameter("@Year",year ), new SqlParameter("@Month",  month ), new SqlParameter("@ProjectID", projectid )).ToList();
        var ClosedBugs = db.ExecuteStoreQuery<LoggedBugs>("ClosedBugs @Year,@Month,@ProjectID", new SqlParameter("@Year", year), new SqlParameter("@Month", month), new SqlParameter("@ProjectID", projectid)).ToList();
        var model = new LoggedBugs
        {
            LoggedBugsCount = loggedbugs,
            ClosedBugs = ClosedBugs
        };
        return Json(model, JsonRequestBehavior.AllowGet);
    }  

这是我的存储过程

      alter procedure LoggedBugs
      (
      @Year int,
      @Month int,
      @ProjectID int
      )
      as
      begin
       SELECT projectName,
    ProjectYear,
    ProjectMonth, 
      Week1, Week2, Week3, Week4, Week5
          FROM (
 Select  p.projectName,YEAR(b.CreatedDate) ProjectYear, MONTH(b.CreatedDate) ProjectMonth,
        'Week' + CAST(DATEDIFF(week, DATEADD(MONTH, DATEDIFF(MONTH, 0, b.CreatedDate), 0), b.CreatedDate)+1 AS VARCHAR) as [Weeks],
        b.BUGID
From    BUGS b inner join projects p on b.ProjectId = p.ProjectId
Where   DatePart(year, CreatedDate) = @Year and datepart(month, Createddate) = @Month and b.projectid = @ProjectID 
  ) p
   PIVOT (COUNT(BUGID) FOR [Weeks] IN (WEEK1, WEEK2, WEEK3, Week4, Week5)) AS pvt
 end

我哪里做错了

编辑1

这是我的 LoggedBug 类

 public class LoggedBugs
{
    public string projectName { get; set; }
    public string ProjectYear { get; set; }
    public string ProjectMonth { get; set; }
    public string Week1 { get; set; }
    public string Week2 { get; set; }
    public string Week3 { get; set; }
    public string Week4 { get; set; }
    public string Week5 { get; set; }
    public IEnumerable<LoggedBugs> LoggedBugsCount { get; set; }
    public IEnumerable<LoggedBugs> ClosedBugs { get; set; }
}

【问题讨论】:

  • 你能显示 LoggedBugs 类的代码,以及 ProjectYear、ProjectMonth、Week1、Week2、Week4、Week4、Week5 的 sql 类型是什么。我的猜测是您的 proc 正在返回一个 int 但 LoggedBugs 将其定义为字符串类型
  • @nuhusky2003 我已经用 LoggedBug 类更新了我的问题,我被转换为 ProjectYear、ProjectMonth、Week1、Week2、Week4、Week4、Week5 的 varchar

标签: asp.net-mvc-3 sql-server-2008 stored-procedures


【解决方案1】:

ProjectYearProjectMonth 可能有问题。在数据库端,它们是int,因此在.NET 端,它们可能会被拆箱为System.Int32。在您的实体中,它们是string。 尝试在您的 sp 中将其转换为 varchar 或将实体中的类型更改为 int

【讨论】:

    【解决方案2】:

    将您的 ProjectYear 和 ProjectMonth 属性更改为在 LoggedBugs 类中键入 int。您的 sql 将它们作为 int 返回,请参阅YEAR SQL MSDN description,因为 YEAR 和 MONTH sql 函数都返回 int 类型的值。您的 C# 代码需要一个字符串,因此您会收到类型不匹配异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多