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

本文关键字:返回 信息 cshtml 存储过程 执行 | 更新日期: 2023-09-27 18:13:38

我之前关于这个主题的问题问得很糟糕,所以我想先问一个更简洁的问题,这才是我问题的核心。在我的.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#项目,我有点急于让它正确工作。

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

这里有几个问题:

  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
                }
            }
        }
    }
    

假设你的模型对象名为"Product",并且有两个属性:"ID"answers"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);