我搜索了在 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/