我的页面中有这段代码:
protected void Page_Load(object sender, EventArgs e)
{
conn.Open();
string selectQuery = @"SELECT AVG(odometer_reading),truck_id FROM
inspection WHERE date BETWEEN '2015-12-01' AND '2016-06-01'
GROUP BY truck_id";
MySqlCommand cmd = new MySqlCommand(selectQuery, conn);
MySqlDataReader reader = cmd.ExecuteReader();
foreach (DbDataRecord rowData in reader)
{
populateTable(rowData);
}
}
private void populateTable (DbDataRecord rowData)
{
TableRow tr = new TableRow();
TableCell cell1 = new TableCell();
cell1.Text = rowData.GetString(rowData.GetOrdinal("AVG(odometer_reading)")).ToString();
tr.Cells.Add(cell1);// Error
TableCell cell2 = new TableCell();
cell2.Text = rowData.GetString(rowData.GetOrdinal("truck_id")).ToString();
tr.Cells.Add(cell2);
tbl_TruckEfficiency.Rows.Add(tr);
}
我收到错误消息:无法将类型为“System.Double”的对象转换为类型为“System.String”。
这可能是什么原因造成的。
或者
是否有其他方法让 AVG(odometer_reading)
将其发送到 cell1.text
而忽略其数据类型以避免错误。谢谢!
最佳答案
试试这个
cell1.Text=rowData["AVG(odometer_reading)"].ToString();
关于c# - 从 MySql 数据库读取平均值。 C# ASP.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33622832/