How to get float value with SqlDataReader?

前端 未结 7 2193
执念已碎
执念已碎 2020-12-20 13:49

In my database, I have NextStatDistanceTime value as a float. When \"float time = reader.GetFloat(0);\" line excecuted, it gives an error of

7条回答
  •  生来不讨喜
    2020-12-20 14:14

     while (reader.Read())
     {
         object initialTime = reader["NextStatDistanceTime"];
         float time;
         float.TryParse(initialTime.ToString(), out time);
    
         totaltime = totaltime + time;
         conn.Close();
     }
    

    Try this, this will get the time from the Database then convert it to a float, you can just put the reader["NextStatDistanceTime] in the tryparse if you want but to make it clearer i have done it like this.

    Any issues let me know

提交回复
热议问题