c# - 尝试在 SQL 中从我的数据库中选择数据时引发 System.IndexOutOfRangeException。ASP.NET
问题描述
我正在尝试从图表中选择并显示我的应用程序中的平均温度。执行我的 SQL 命令后,我希望在图表中显示平均温度,但它会抛出:
System.IndexOutOfRangeException:'温度'
这是我拥有的代码。如果有人可以帮助我,我将不胜感激。
private void Database(string location, string temperature, string unit)
{
SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\MyPC\source\repos\NHLweatherStation\NHLweatherStation\Database1.mdf;Integrated Security=True");
connection.Open();
SqlCommand sqlCommand = new SqlCommand("INSERT INTO Weather (location,Temperature,date,unitsTemperature) VALUES ('" + location + "','" + temperature + "','" + DateTime.Now + "','" + unit + "')", connection);
sqlCommand.ExecuteNonQuery();
connection.Close();
SqlCommand cmd = new SqlCommand("Select avg(Temperature),date From Weather Group by date", connection);
connection.Open();
SqlDataReader rdr = cmd.ExecuteReader();
TrendGraph.Series["Series1"].XValueMember = "date";
TrendGraph.Series["Series1"].YValueMembers = "Temperature";
TrendGraph.DataSource = rdr;
TrendGraph.DataBind();
connection.Close();
}
解决方案
推荐阅读
- python - 将numpy数组强制为相同的维度
- c++ - 如何为模板类添加运算符<<?
- java - 检查 ArrayList 中的所有单词是否都在字符串数组中并按顺序排列?
- node.js - 快速会话用户名更改与最新会话加入
- pip - 让 pipenv 在应用程序工作的 Virtualenv 中工作
- docker - Docker compose 不会为链接服务设置公开端口 80
- java - 使用 listview 并为下一节课添加数据
- ruby - 只包含偶数的循环
- ios - 'UIViewController' 不是 'View Controller' Swift 的子类型
- operating-system - RTOS eCos 使用什么内核类型?