【问题标题】:sum together values in database column汇总数据库列中的值
【发布时间】:2013-04-03 06:31:12
【问题描述】:

我正在asp.net 上用c# 创建一个Web 项目。我正在网页上显示来自数据库的数据。我有一个数据库表,其中一列(月份)显示客户在哪个月份下订单,另一列显示库存 ID。每个月和 stockid 都会显示很多次,因为每个月都有不同的客户下订单。我想汇总特定库存每个月的数量列中的订单数量。目前我只能在循环时显示每个月的一个数量,但想计算并显示每个月的总数。

    public static ArrayList GetActuals()
    {
        String strQuery = "Select * from Actual where Year = 2013";
        Recordset rs = DatabaseManager.GetRecordset("DB", strQuery);

        bool bFound;

        ArrayList Actuals = new ArrayList();
        while (rs != null && rs.Read())
        {
            Actual A = new Actual();
            A.strStockNo = rs.GetFieldValueString("Stock_No").Trim();
            A.nMonth = rs.GetFieldValueInt("Month");
            A.nYear = rs.GetFieldValueInt("Year");
            A.nCustomer = rs.GetFieldValueInt("Customer");
            A.nQuantity = (float)rs.GetFieldValueDouble("Quantity");
            Actuals.Add(A);

        }

        if (rs != null) rs.Close();
        return Actuals;

    }



    float LoadActuals(ArrayList actual, String strstock, int year, int month)
    {

        foreach (Actual a in actual)
        {

            if ((a.strStockNo == strstock) && (a.nYear == year) && (a.nMonth == month))
            {
                return a.nQuantity;
            }


        } return 0;
    }

然后当我显示每个月的数量时......

            int Month;
            for (Month = 1; Month < 13; Month++)
            {

                    float totq = LoadActuals(Act, p.strStockNo, yr, Month);

                    TableCell cell = new TableCell();
                    cell.Text = string.Format("{0}", totq);
             }

这仅显示每个月的一个 totq,因为我想要总数。这是怎么做到的?

【问题讨论】:

  • 很适合包含一个解释挫折的链接,但它们在这里显然是新的,所以请尽量表现得友善和建设性。
  • 您希望在查询或 C# 代码中使用哪个数据库?
  • 在我的 c# 代码中。我有一个数组列表,它存储查询的结果,但是当在网页上的特定列下显示它们时,即 jan、feb、mar ... 它只显示一个结果,我想显示每个月的总值?
  • @newbie1 不要描述你的代码,而是发布一些最小的、独立的版本并提出一个特定的问题。

标签: c# asp.net loops sum


【解决方案1】:

这么多问题,但...很快。

float LoadActuals(ArrayList actual, String strstock, int year, int month)
{
    float quantity = 0;
    foreach (Actual a in actual)
    {
        if ((a.strStockNo == strstock) && (a.nYear == year) && (a.nMonth == month))
        {
            quantity += a.nQuantity;
        }
    }
    return quantity;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 2020-07-13
    • 1970-01-01
    • 2017-09-07
    相关资源
    最近更新 更多