Float in Database to ? in .NET
-
23-08-2019 - |
Question
If you have a float in MSSQLServer, to what do you map this in .NET?
Can you convert it to Double or will you lose numbers?
Solution
SQLServer float
and C#/VB double
have the same representation. This is the correct mapping. What you don't want to do is map SQL Server float
to C#/VB float
as that may involve a loss of precision. SQL Server real
maps onto C#/VB float
.
T-SQL float and real type definitions can be found at MSDN. C# double definition can be found at MSDN as well, as can the float definition.
OTHER TIPS
Check out:
- SQL Server Data Types and Their .NET Framework Equivalents (msdn) for full reference on data type mappings between SQL Server and .Net
In your case SQL Server native type float maps to SQL Server CLR SqlDouble and then Double in .Net
Update (May 2016):
Updated version of the msdn document: Mapping CRL Parameter Data
C# has a float type, but it will convert to double just fine as well.
From my recollection, most of the ORM tools will map it to a Decimal type, which will not lose precision like a double or float.
It depends on the size of the SQL float. For plain "float" which is equivalent to float(53), you need to use a C# double. For float(24) or lower a C# float will be enough, or a double would work as well.
The automatic code generated by Microsoft XSD convert SQL float (default float) to C# double.
So you can suggest that it isn't big mistake.
Until you working with float that have default size.