How to get float value using SqlDataReader?

In my database, NextStatDistanceTime is float. When the string " float time = reader.GetFloat(0); " is canceled, it gives an error

mold casting exception

How can I get the float value from sql command in this code?

Here is my code:

 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); } } } 
+6
source share
7 answers

This is the time for a small table, I think.

 T-SQL type name | .NET equivalent | C# type name | DataReader method ----------------+-----------------+--------------+------------------------ FLOAT | System.Double | double | IDataReader.GetDouble() REAL | System.Single | float | IDataReader.GetFloat() 

Note that GetFloat has the wrong name - it must be GetSingle , because float is a C # -specific name. For example, this does not make sense in VB.NET.

So, if the database column is of type float , read it using GetDouble , not GetFloat . Data reading methods do not perform conversions; There is a common GetValue method to get the value as an object , which can then be converted further.

By the way, this is not the only subtlety - .NET floating point types support denormalized values , while T-SQL types do not, therefore in your .NET code you can have floating point numbers that cannot be stored in the database, even if the types match up.

+12
source

As you can read here , the sql-server float server maps to .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 } 
+3
source

I assume that the database returns a double value, try to get it as Double and convert it to float (if required).

 float time= (float) reader.GetDouble(0); 
+1
source

you can try:

 float time = float.Parse(reader[0].ToString()); 

also note (albeit not related to your Q) that you do not need to run

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

Try this, it will give time from the database and then convert it to float, you can just put reader["NextStatDistanceTime] in tryparse if you want, but to make it more clear, I made it like this.

Any questions let me know

0
source

try it

 convert.ToSingle(reader["NextStatDistanceTime"]) 

or do

 double value = (double)reader["NextStatDistanceTime"] 

Float of sql is equivalent to double from C #, you can see a similar display here

0
source

This is probably a precision mismatch between the database type and the C # type. Try to do as (float)reader.GetDouble(0);

0
source

All Articles