c# - Mysql数据库列数的图表

标签 c# mysql count bar-chart

我搜索了在 C# 中绘制 sql 计数函数的图表。我正在尝试绘制一个表格,该表格计算每个行标签的条目并在条形图上显示计数。例如:

-Foreman    -count(CommentOne)
   Me                3
   you               2

我正在使用这个:

   chart1.Series.Clear();
   chart1.Series.Add("Series1");
   string Query = "select Foreman,count(CommentOne) from database.scaffoldqcdata where Location='" + cb_location + "' Group by Foreman;";
   MySqlConnection conDataBase = new MySqlConnection(constring);
   MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
   MySqlDataReader myReader;
   try
   {
       conDataBase.Open();
       myReader = cmdDataBase.ExecuteReader();
       while (myReader.Read())
       {
           this.chart1.Series["Series1"].Points.AddXY(myReader.GetString("Foreman"), myReader.GetString("CommentOne"));
       }

   }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

它曾与其他查询一起使用,但当我运行此查询时它显示空白。

最佳答案

您需要使用列别名在查询中定义CommentOne:

select Foreman, count(CommentOne) as CommentOne
from database.scaffoldqcdata
where Location='" + cb_location + "'
Group by Foreman

老实说,我建议不要使用那个名字。相反,像这样:

select Foreman, count(CommentOne) as NumComments
from database.scaffoldqcdata
where Location='" + cb_location + "'
Group by Foreman

并在图表代码中使用这个名称:

this.chart1.Series["Series1"].Points.AddXY(myReader.GetString("Foreman"), myReader.GetString("NumComments"));

如果您希望结果按特定顺序排列,您可能还想包含一个order by。您不应该依赖于 group by 来进行任何特定的排序。

关于c# - Mysql数据库列数的图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27877110/

相关文章:

c# - 如何在 C# 中从带有过渡的图像创建视频?

c# - 使用 mySQL INSERT 执行命令时遇到 fatal error

php - 在 Laravel 中创建数据库约束断言

php - UTF-8贯穿始终

mysql - 不同条件下的多选计数

django - django中按字段类型统计数据库条目

c# - 如何在 C# Winforms 中向标签添加提示或工具提示?

mysql - DATEDIFF(CURRENT_DATE, updated_at) 作为差异,COUNT(*)

android - 计算 sd 卡中的图像

c# - 如何找到由下划线限定的 5 个数字组成的变量集?