SQL返回脚本未返回数据

时间:2019-07-11 17:32:57

标签: sql-server asp.net-core

我有一个不返回数据的Sql Server脚本。我的.net变量返回为null。我还有其他以相同方式构建的脚本,它们可以正常运行。

    public List<float> FlowerTotal { get; set; }
    public List<float> TrimTotal { get; set; }
    public List<float> WasteTotal { get; set; }
    public List<int> result = new List<int>();
    public List<List<float>> resultTotal = new List<List<float>>();

    public List<List<float>> TotalReturns(int FileID)
    {
        using (SqlConnection myConn = new SqlConnection(cs))
        {
            SqlCommand returnTotal = new SqlCommand();
            returnTotal.Connection = myConn;
            myConn.Open();

            returnTotal.Parameters.AddWithValue("@fileID", FileID);

            returnTotal.CommandText = ("[spHarvestedCannabisTotalReturn]");
            returnTotal.CommandType = CommandType.StoredProcedure;
            using (SqlDataReader reader = returnTotal.ExecuteReader())
            {
                 while (reader.Read())
                 {
                     if(FlowerTotal != null)
                         {
                              FlowerTotal.Add(reader.GetFloat(0));
                              TrimTotal.Add(reader.GetFloat(1));
                              WasteTotal.Add(reader.GetFloat(2));
                           }
                  }
                      resultTotal.Add(FlowerTotal);
                      resultTotal.Add(TrimTotal);
                      resultTotal.Add(WasteTotal);
                      reader.Close();          
            }
            returnTotal.Cancel();
            myConn.Close();
            return resultTotal;
        }
     }

    [spHarvestedCannabisTotalReturn]
        @fileID int
        AS
        BEGIN
        DECLARE
            @flower FLOAT,
            @trim FLOAT,
            @waste FLOAT

                SELECT [Flowers(F)], [Trim(T)], WasteMaterial FROM 
        dbo.HarvestedCannabis
                WHERE FileID = @fileID

                RETURN @flower
                RETURN @trim
                RETURN @waste

        END

我下面的变量返回为null,我在做什么错?在正确方向上的任何帮助都是很好的

FlowerTotal.Add(reader.GetFloat(0));
TrimTotal.Add(reader.GetFloat(1));
WasteTotal.Add(reader.GetFloat(2));

1 个答案:

答案 0 :(得分:0)

if(FlowerTotal != null)行阻止将结果添加到列表<>。确保所有列表<>已初始化。

我认为您可以删除,将sp简化为:

[spHarvestedCannabisTotalReturn]
@fileID int
AS
BEGIN
        SELECT [Flowers(F)], [Trim(T)], WasteMaterial
        FROM   dbo.HarvestedCannabis
        WHERE  FileID = @fileID
END

未使用该变量public List<int> result = new List<int>();

为什么这里有list<list<>>public List<List<float>> resultTotal = new List<List<float>>();