【问题标题】:Standard HTML table with total from SQL Server table标准 HTML 表格,包含来自 SQL Server 表格的总数
【发布时间】:2014-08-28 19:13:28
【问题描述】:

参考此链接:SQLDataReader GetDateTime Format

结果应该是:

ID     Number01     TheDate     Number02
----------------------------------------
1      10           01/06/2014  5
2      20           02/06/2014  10
3      30           03/06/2014  15

有人可以告诉我如何将总计放在包含数字数据类型的每一列上吗?总数应该在表格的页脚。

例如,第一个页脚,是SUM(number02),那么第二个页脚,就是平均值。 AVG(number02)

所以我可以说,多页脚。

结果应该是:

ID     Number01     TheDate     Number02
----------------------------------------
1      10           01/06/2014  5
2      20           02/06/2014  10
3      30           03/06/2014  15
TOTAL  60           -           30
AVE    20           -           10

请帮忙。谢谢。

【问题讨论】:

    标签: c# html asp.net sql-server sql-server-2008


    【解决方案1】:

    你可以使用这个html

    <tfoot>
        <tr>
          <td>Tot</td>
          <td>60</td>
          <td></td>
          <td>30</td>
        </tr>
        <tr>
           <td>Avg</td>
           <td>20</td>
           <td></td>
           <td>10</td>
        </tr>
      </tfoot>
    

    这会在表格末尾添加两行。

    计算总和平均 在定义中

    int totnum1 = 0;
    decimal totnum2 = 0;
    int numRow = 0;
    decimal avg1 = 0;
    decimal avg2 = 0;
    

    在循环中

    totnum1 += reader.GetInt32(1);
    totnum2 += reader.GetInt32(3);
    numRow ++;
    

    循环结束

    avg1 = totnum1 / numRow;
    avg2 = totnum2 / numRow;
    

    您可以使用 totnum1、totnum2 avg1 和 avg2 代替上例中的数字来编写上一个问题中的 html

    public string getWhileLoopData() 
    {
     string htmlStr = "";
     SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
     SqlCommand thisCommand = thisConnection.CreateCommand();
     thisCommand.CommandText = "SELECT * FROM MyTable WHERE TheDate = @TheDate";
     thisCommand.Parameters.AddWithValue("@TheDate", txtDate.Text);
    
    
    int totnum1 = 0;
    decimal totnum2 = 0;
    int numRow = 0;
    decimal avg1 = 0;
    decimal avg2 = 0;
    
    
    
     thisConnection.Open();
     SqlDataReader reader = thisCommand.ExecuteReader();
    
     while (reader.Read()) {
         int id = reader.GetInt32(0);
    
         int Number01 = reader.GetInt32(1);
         DateTime TheDate = reader.GetDateTime(2);
         Decimal Number02 = reader.GetDecimal(3);
    
         totnum1 += reader.GetInt32(1);
         totnum2 += reader.GetInt32(3);
         numRow ++;
    
         //string Pass = reader.GetString(2);
         htmlStr += "<tr><td>" + id + "</td><td>" + Number01 + "</td><td>" + TheDate + "</td><td>" + Number02 + "</td></tr>";
     }
    
     thisConnection.Close();
    
    avg1 = totnum1 / numRow;
    avg2 = totnum2 / numRow;
    
    htmlStr += string.Format("<tfoot><tr><td>Tot</td><td>{0}</td><td></td><td>{1}</td></tr>", totnum1 , totnum2 );
    htmlStr += string.Format("<tfoot><tr><td>Avg</td><td>{0}</td><td></td><td>{1}</td></tr></tfoot>", avg1 , avg2 );
    
    
     return htmlStr;
    }
    

    【讨论】:

    • 嘿伙计,我没听懂你。您能否将您的代码添加到以前的代码中并在此处提交?干杯,
    • “/”应用程序中的服务器错误。试图除以零。第 84 行:thisConnection.Close();第 85 行:第 86 行:avg1 = totnum1 / numRow;第 87 行:avg2 = totnum2 / numRow;
    • 有多少行?如果为0,那么可以在除法前加一个条件(if(numRow > 0)...
    • numRow++ 是什么意思?如果我有 10 条记录,并且 7 条记录等于零。平均值计算所有记录。应该只计算非零值的 3 条记录。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2010-10-28
    • 1970-01-01
    • 2013-12-10
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多