我有这个SQL查询,当我在SQL Management Studio中运行它时可以使用。

select Products.ProductName, ProductTypes.ProductType, count(SaleID) numSales
from Products
join ProductTypes on ( ProductTypes.ProductTypeID= Products.ProductTypeID)
join Sales on ( Sales.ProductID = Products.ProductID)
group by Products.ProductName, ProductTypes.ProductType

它返回该表。

在此处输入图片说明

我正在将其与Visual Studio数据网格视图集成在一起,并且正在努力使numSales列正常工作,现在对于这样的所有事情它仅显示0。

在此处输入图片说明

这是我用于填充表格的代码

private void DisplayDashboard()
        {
            string numSalesQuery = "select Products.ProductName, ProductTypes.ProductType, count(SaleID) numSales " +
                "from Products join ProductTypes on (ProductTypes.ProductTypeID = Products.ProductTypeID)" +
                "join Sales on (Sales.ProductID = Products.ProductID)" +
                "group by Products.ProductName, ProductTypes.ProductType";

            List<_Dashboard> dashList = new List<_Dashboard>();
            try
            {
                // Automatically  open and close the connection
                using (var conn = ConnectionManager.DatabaseConnection())
                using (var cmd = new SqlCommand(numSalesQuery, conn))
                using (var rdr = cmd.ExecuteReader())
                {

                    while (rdr.Read())
                    {
                        //Define the list items
                        var dashboard = new _Dashboard(
                            rdr["ProductName"].ToString(),
                            rdr["ProductType"].ToString(),
                            int.Parse(rdr["numSales"].ToString()));
                        dashList.Add(dashboard);
                    }
                    dgvDashboard.DataSource = dashList;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unsuccessful" + ex);
            }

        }

感谢您提供有关此问题的帮助。

编辑:这是_Dashboard类

namespace Acme_Project.Business_Logic_Layer
{
    public class _Dashboard
    {
        //Declare properties of a Customer

        public String ProductName { get; set; }
        public string ProductType { get; set; }
        public int NumSales { get; set; }

        //Declaring Default Constructor
        public _Dashboard() { }

        //Parameterised Constructor
        public _Dashboard(string productname, string productype, int numsales)
        {
            ProductName = productname;
            ProductType = productype;
            NumSales = NumSales;
        }
    }
}

===============>>#1 票数:1 已采纳

设置NumSales = NumSalesNumSales = numsales您的参数化构造函数中

  ask by Rick1990 translate from so

未解决问题?本站智能推荐: