【问题标题】:Why Database.SqlQuery is returning 0 rows?为什么 Database.SqlQuery 返回 0 行?
【发布时间】:2019-04-15 10:31:18
【问题描述】:

我用过这个

        public List<InspectionReport> GetInspectionReportDetails(int InspectionReportID)
        {
            var List= this.Database.SqlQuery<InspectionReport>("GetInspectionReportDetails", InspectionReportID).ToList();

            return List;
        }

在继承自DbContext的类中

我在控制器中调用此函数,但 List 为空。 Database.SqlQuery 返回 0 个项目,即使过程正在为我提供给函数的参数返回数据,但不是这个。

更新:

alter PROCEDURE GetInspectionReportDetails 

    @InspectionReportID int=0

AS
BEGIN

    Select ir.InspectionReportID
          ,ir.VelosiProjectNo
          ,ir.VelosiReportNo   
          ,ir.Reference
          ,ir.PoNo
          ,ir.InspectionDate
          ,ir.IssueDate
          ,ir.InspectionPhase
          ,ir.InServiceInspection
          ,ir.NewInduction
          ,ir.HydrostaticTest
          ,ir.DimensionalCheck
          ,ir.ThicknessCheck
          ,ir.Patrom
          ,ir.Gvs
          ,ir.FinalOgraInspection
          ,ir.OmcClientRequirement
          ,ir.TankLorryRegistrationNo
          ,ir.TruckTractorManufacturerName
          ,ir.ClientName
          ,ir.Capacity
          ,ir.Omc
          ,ir.EngineNo
          ,ir.TankLorryDimension
          ,ir.ChassisNo
          ,ir.InspectionPlace
          ,ir.TankLorryEnginePower
          ,ir.CarriageName
          ,ir.Brakes
          ,ir.IsSatisfactory
          ,ir.Remarks
          ,ir.Rev
          ,ir.Description
          ,ir.Status
          ,u1.UserName as PeparedBy
          ,u2.UserName as CheckedBy
          ,u3.UserName as ApprovedBy
          ,u4.UserName as IssuedBy
      From InspectionReport ir
      Inner Join dbo.[User] u1
      ON u1.UserID= ir.PeparedBy
      Inner Join dbo.[User] u2
      ON u2.UserID= ir.CheckedBy
      Inner Join dbo.[User] u3
      ON u3.UserID= ir.ApprovedBy
      Inner Join dbo.[User] u4
      ON u4.UserID= ir.IssuedBy

      where ir.InspectionReportID= @InspectionReportID

这是函数调用的存储过程。

这个类在psectionreport;

namespace VAILCertificates.DAL.Entities
{
    public class InspectionReport
    {
        //General Details
        public int InspectionReportID { get; set; }

        [Required]
        [Display (Name= "Velosi Project No")]
        public string VelosiProjectNo { get; set; }

        [Display(Name = "Velosi Report No")]
        public string VelosiReportNo { get; set; }

        [Required]
        public string Reference { get; set; }

        [Required]
        [Display(Name = "PO No")]
        public string PoNo { get; set; }

        [Required]
        [Display(Name = "Inspection Date")]
        public DateTime InspectionDate { get; set; }

        [Required]
        [Display(Name = "Issue Date")]
        public DateTime IssueDate { get; set; }

        //Inspection Phase
        [Required]
        [Display(Name = "Inspection Phase")]
        public byte InspectionPhase { get; set; } //0= Before, 1= During, 2= Final

        //Types Of Inspection
        [Display(Name = "In Service Inspection")]
        public bool InServiceInspection { get; set; }

        [Display(Name = "New Induction")]
        public bool NewInduction { get; set; }

        [Display(Name = "Hydorstatic Test")]
        public bool HydrostaticTest { get; set; }

        [Display(Name = "Dimensional Check")]
        public bool DimensionalCheck { get; set; }

        [Display(Name = "Thickness Check")]
        public bool ThicknessCheck { get; set; }

        [Display(Name = "PATROM")]
        public bool Patrom { get; set; }

        [Display(Name = "GVS")]
        public bool Gvs { get; set; }

        [Display(Name = "Final OGRA Inspection")]
        public bool FinalOgraInspection { get; set; }

        [Display(Name = "OMC/Client Requirement")]
        public bool OmcClientRequirement { get; set; }

        //Details Of Tank Lorry

        [Required]
        [Display(Name = "Tank Lorry Registration Number")]
        public string TankLorryRegistrationNo { get; set; }

        [Required]
        [Display(Name = "Truck Tractor Manufacturer Name")]
        public string TruckTractorManufacturerName { get; set; }

        [Required]
        [Display(Name = "Client Name")]
        public string ClientName { get; set; }

        [Required]
        [Display(Name = "Capacity")]
        public string Capacity { get; set; }

        [Required]
        [Display(Name = "OMC")]
        public string Omc { get; set; }

        [Required]
        [Display(Name = "Engine No")]
        public string EngineNo { get; set; }

        [Required]
        [Display(Name = "Tank Lorry Dimension")]
        public string TankLorryDimension { get; set; }

        [Required]
        [Display(Name = "Chassis Number")]
        public string ChassisNo { get; set; }

        [Required]
        [Display(Name = "Inspection Place")]
        public string InspectionPlace { get; set; }

        [Required]
        [Display(Name = "Tank Lorry (Engine Power)")]
        public string TankLorryEnginePower { get; set; }

        [Required]
        [Display(Name = "Carriage Name")]
        public string CarriageName { get; set; }

        [Required]
        public string Brakes { get; set; }

        //ResultsofInspection

        [Required]
        [Display(Name = "Is Satisfactory?")]
        public bool IsSatisfactory { get; set; }

        //Remarks
        [Required]
        public string Remarks { get; set; }

        //ApprovalSection

        public string Rev { get; set; }

        [Required]
        public string Description { get; set; }

        [Required]
        public int PeparedBy { get; set; }

        public int CheckedBy { get; set; }
        public int ApprovedBy { get; set; }

        public int IssuedBy { get; set; }

        //ReportStatus

        public byte Status { get; set; } //0= Prepared 1= CheckedBy 2= ApprovedBy

    }
}

【问题讨论】:

  • 你能发布 InspectionReport 的代码和 SP 的示例输出吗?
  • 请检查更新的问题
  • InspectionReport 的班级在哪里?
  • 已更新,请检查
  • 你检查了吗?

标签: c# entity-framework c#-4.0 entity-framework-6


【解决方案1】:

您还可以构造一个 DbParameter 并将其作为参数值提供给您的存储过程:

var InspectionReportID= new SqlParameter("@InspectionReportID", 0);

var list = this.Database.SqlQuery<InspectionReport>("EXECUTE dbo.GetInspectionReportDetails @InspectionReportID",InspectionReportID).ToList();

参数化任何用户输入以防止 SQL 注入攻击非常重要。您可以在 SQL 查询字符串中包含参数占位符,然后提供参数值作为附加参数。您提供的任何参数值都将自动转换为DbParameter。

【讨论】:

  • 在这一行:$"EXECUTE dbo.GetInspectionReportDetails @InspectionReportID",InspectionReportID
  • @PostingQuestions 编辑了我的答案。现在可以试试吗?
  • 再次出错;必须声明标量变量“@InspectionReportID”。
  • 在您的 S.P 中,将您的参数声明为:@InspectionReportID int 并从您的 C# 中提供值
  • @PostingQuestions 您在 S.P 中始终将其设置为 0。只需声明参数即可。
【解决方案2】:

改sql语句试试,感觉是你调用sql语句的方式出错。

var list = this.Database.SqlQuery<InspectionReport>($"EXEC GetInspectionReportDetails {InspectionReportID}").ToList();

【讨论】:

  • ?你想清楚了吗?
  • 这一行:($"EXEC GetInspectionReportDetails {InspectionReportID}")
  • 不知道为什么编译出错,可能是去掉字符串插值试试("EXEC GetInspectionReportDetails " + InspectionReportID.ToString())
  • “尝试”、“感觉”……这并不是真正的答案。如果您仍处于假设阶段,请发表评论,或者在发布答案时使用更自信的措辞。请记住,Stack Overflow 上的问题和答案将被许多其他寻求答案的用户阅读。
  • 这应该组成为:var list = this.Database.SqlQuery&lt;InspectionReport&gt;($"EXEC GetInspectionReportDetails ({InspectionReportID})").ToList();
【解决方案3】:

尝试使用此代码:

public List<InspectionReport> GetInspectionReportDetails(int InspectionReportID)
{
    var List= this.Database.SqlQuery<InspectionReport>("GetInspectionReportDetails @InspectionReportID", InspectionReportID).ToList();
    return List;
}

【讨论】:

  • 错误; Message = "必须声明标量变量\"@InspectionReportID\"。"
  • 请在另一个答案上查看我的comment。此外,我们总是感谢您提供简短的解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-21
  • 2022-01-23
  • 1970-01-01
  • 2011-03-01
  • 2011-07-01
  • 2011-01-17
  • 2018-04-05
相关资源
最近更新 更多