2013-04-02 207 views
0

我在asp.net上的c#中創建了一個Web項目。我在網頁上顯示來自數據庫的數據。我有一個數據庫表,其中一列(月份)顯示客戶在哪個月下單,另一列顯示股票編號。由於不同的客戶每個月都會下訂單,所以每個月和每個小時都會顯示多次。我希望從特定股票的每個月的數量列中總計訂單數量。目前,我只能顯示每個月的數量,當它循環但想要計數並顯示每月的總數。將數據庫列中的值相加在一起

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每個月其中作爲我想要的總。這是如何完成的?

+4

http://mattgemmell.com/2008/12/08/what-have-you-tried/ – walther

+0

適合包括一個鏈接解釋挫折,但他們顯然是新來的所以不要嘗試是好的和建設性的。 – Clint

+0

哪個數據庫,你想這在你的查詢或在你的C#代碼? – Kashif

回答

0

很多問題,但......很快。

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; 
} 
相關問題