在Web API中执行存储过程时获取空值,但在MSSQL中获取结果

时间:2018-12-06 06:51:20

标签: c# sql-server

我有一个存储过程,该输出过程在MSSql Server中为我提供了此输出 enter image description here

所以我创建了一个类来捕获此输出,如下所示:

public class OwnerInfo
{
    public string UnitName { get; set; }
    public string UnitOwner { get; set; }
}
public class FirstInfo
{
    public string TType { get; set; }
    public string TransactionType { get; set; }
    public string Period_Covered { get; set; }
    public double? BillAmount { get; set; }
    public double? PaymentAmount { get; set; }
    public double? Balance { get; set; }
}
public class SecondInfo
{
    public string TType { get; set; }
    public string TransactionType { get; set; }
    public string Period { get; set; }
    public double? BillAmount { get; set; }
    public double? PaymentAmount { get; set; }
    public double? Balance { get; set; }
}
public class ThirdInfo
{
    public string TType { get; set; }
    public string TransactionType { get; set; }
    public string Period { get; set; }
    public double? PaymentAmount { get; set; }
}
public class FourthInfo
{
    public string TType { get; set; }
    public string Particulars { get; set; }
    public string PaymentDate { get; set; }
    public double? PaymentAmount { get; set; }
    public string ORNumber { get; set; }
}
public class DisplaySoa
{
    public OwnerInfo OwnerInfo { get; set; }
    public List<FirstInfo> FirstInfo { get; set; }
    public List<SecondInfo> SecondInfo { get; set; }
    public List<ThirdInfo> ThirdInfo { get; set; }
    public List<FourthInfo> FourthInfo { get; set; }
}
public class DisplaySoaParameter
{
    public int UnitID { get; set; }
    public int Month { get; set; }
    public int Year { get; set; }
}

从数据库中获取数据的方式是在控制器中,然后像这样调用存储过程:

const string query = "[dbo].[DISPLAY_SOA_V4] @UnitID, @Month, @Year";
            using(var db = new ApplicationDbContext())
            {

                try
                {
                    //const string query = "[dbo].[DUES_AUTO_PAYMENT] @PaymentAmount @PaymentTypeID @ORNumber @UnitID @CurrentDateTime";
                    object[] parameter =
                    {
                        new SqlParameter
                        {
                            ParameterName = "@UnitID",
                            Value = model.UnitID,
                            Direction = ParameterDirection.Input,
                            SqlDbType = SqlDbType.Int
                        },
                        new SqlParameter
                        {
                            ParameterName = "@Month",
                            Value = model.Month,
                            Direction = ParameterDirection.Input,
                           SqlDbType = SqlDbType.Int
                        },
                        new SqlParameter
                        {
                            ParameterName = "@Year",
                            Value = model.Year,
                            Direction = ParameterDirection.Input,
                            SqlDbType = SqlDbType.Int
                        }
                    };

                    var x = db.Database.SqlQuery<DisplaySoa>(query, parameter).ToList();
                    return x;
                }
                catch (Exception ed)
                {
                    return null;
                }
            }

但是在我的“ x”值中,我得到null像这样: enter image description here

该图像是当我使用.ToList()时使用.FirstOrDefault()bur时,我的计数为1,但是没有任何意义。您能帮我这个忙吗?谢谢。

1 个答案:

答案 0 :(得分:1)

您的sp返回多个结果集。

var x = db.Database.SqlQuery<DisplaySoa>(query, parameter).ToList();

由此您无法处理。

请阅读this文章,了解操作方法。