执行存储过程并使用cshtml中返回的信息

时间:2014-07-18 01:15:31

标签: c# asp.net-mvc asp.net-mvc-4 razor

我之前关于这个问题的问题很少被问到,所以我想首先提出一个更简洁的问题,这个问题确实是我问题的核心。在我的.cs模型中,我必须调用一个存储过程来调出每个项目的任务数量。

我需要的具体事项是每个项目可能具有的任务数量,这些任务将作为sp参数UnfinishedTaskCount返回。

我的cshtml文件中的剃刀会显示返回的信息,这是我设法弄清楚的另一个问题,但获取我想要的信息是最后的障碍。这是.cs文件中的代码,我需要对存储过程进行正确的调用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;

namespace IntelliBase.Models
{
    public partial class Project:Description
    {
       ....

        private int unfinishedtasks;
        public int UnFinishedTaskCount
        {
            get
            {
                SqlConnection sqlConnection1 = new SqlConnection("Data Source=ORLANDOSQL1;Integrated Security=True");
                SqlCommand cmd = new SqlCommand();
                Object returnValue;

                cmd.CommandText = "mp_Display_Projects_Home";
                cmd.Parameters.Add("@BeginPage", SqlDbType.VarChar).Value = 0;
                cmd.Parameters.Add("@EndPage", SqlDbType.VarChar).Value = 0;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = sqlConnection1;

                sqlConnection1.Open();
                returnValue = cmd.ExecuteScalar();
                sqlConnection1.Close();

                return unfinishedtasks = (int)returnValue;
            }
            set { ; }
        }

    }
}

ExecuteScalar命令只会带回第一行信息,这没有用,因为有更多的项目行,每行都有自己的任务集,我读到ExecuteNonQuery不会返回特定的参数,只是所有的数据;我不需要所有数据,只需要每个项目的UnfinishedTaskCount。

因此,由于ExecuteScalar,它为表中所有项目的任务数返回“2”,因为第一行中的项目有两个任务(其他项目具有不同的任务数)。是否有另一个Execute--调用将为每个项目获取正确数量的任务,以便我可以在cshtml上正确显示它们?我感谢每个人的耐心;这是我的第一个大型C#项目,我有点急于让它正常工作。

1 个答案:

答案 0 :(得分:2)

这里有几个问题:

  1. 您的模型不应该触及数据库。这是控制器的工作。
  2. 您需要从存储过程中读取整个结果集,并使用它来填充模型。如果每个产品有一行,那么每行会有一个模型对象。使用ExecuteReader,而不是ExecuteScalar
  3. 您的代码中有多个实现IDisposable的对象,因此它们应位于using块中:

    using (SqlConnection sqlConnection1 = new SqlConnection(
                            "Data Source=ORLANDOSQL1;Integrated Security=True"))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read()
                {
                    // Create an instance of your model object,
                    // fill it from the reader, then add it to the collection
                    // of model objects
                }
            }
        }
    }
    

  4. 假装您的模型对象被调用"产品"并有两个属性," ID"和#34; NumberOfUnfinishedTasks"。假装您的SP返回了一组{ID,NumberOfUnfinishedTasks}。然后

    List<Product> products = new List<Product>();
    while (reader.Read()
    {
        // Create an instance of your model object,
        Product product = new Product();
    
        // fill it from the reader
        product.ID = (int) reader["ID"];
        product.NumberOfUnfinishedTasks = (int) reader["NumberOfUnfinishedTasks"];
    
        // then add it to the collection
        // of model objects
        products.Add(product);
    }
    

    随后:

    return View(products);