在我的数据库中,我将NextStatDistanceTime值设置为浮点型。当执行"float time = reader.GetFloat(0);
"代码时,它会报错:
系统无效转换异常
我该如何在这段代码中从SQL命令获取浮点值?
以下是我的代码:
using (SqlConnection conn = new SqlConnection(@"<myconnectionstring>"))
{
float totaltime = 0;
for (int i = startStationIndex; i < endStationIndex; i++)
{
SqlCommand command = new SqlCommand("SELECT NextStatDistanceTime FROM [MetroDatabase].[dbo].[MetroStation] WHERE StationIndex = " + i + "", conn);
try
{
conn.Open();
command.ExecuteNonQuery();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
float time = reader.GetFloat(0);
totaltime = totaltime + time;
conn.Close();
}
}
}
catch (Exception ex)
{
result = ex.Message;
Console.WriteLine(ex.Message);
}
}
}