38

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?

6 Answers 6

80

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Wrong, though you can map any SQL-server float to a C# double, you can't map any C# double to a SQL-server float... But it usually works for "normally" large/tiny numbers
This isn't strictly true - even for REAL/FLOAT(24). SQL Server has no notion of NaN, or +/-Inf which are supported under standard IEEE754 (C#/VB.NET) rules.
16

Check out:

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

2 Comments

This should be the correct answer. Thanks for the link. There is an updated version by now. msdn.microsoft.com/en-us/library/ms131092%28v=sql.130%29.aspx
thanks @MatthisKohli - added the new link to the answer now
0

C# has a float type, but it will convert to double just fine as well.

Comments

0

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.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.