How to get float value with SqlDataReader?

前端 未结 7 2281
执念已碎
执念已碎 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:07

    As you can read here a sql-server float maps to a .NET double, so you need to use GetDouble:

    double totaltime = 0;  // necessary, double is wider than float
    // ...
    
    while (reader.Read())
    {
        double time = reader.GetDouble(0);
        totaltime = totaltime + time;
        // conn.Close(); no, not in this loop, should be closed in the finally or via using-statement
    }
    

提交回复
热议问题