53

What is the C# equivalent to the sql server 2005 real type?

Kevin Fairchild
  • 10,891
  • 6
  • 33
  • 52

7 Answers7

80

it is a Single

see here for more info on SQL Server to .Net DataTypes

YonahW
  • 15,790
  • 8
  • 42
  • 46
  • 2
    Why was this answer not accepted? This is not how the community should be working. Too many questions are going weeks with no accepted answer. – Pittsburgh DBA Oct 06 '08 at 22:26
  • 5
    Who cares? The author didn't care to bother, but we as the community don't need their blessing to see and influence the "best" answer. – Sean Hanley Feb 25 '10 at 15:54
  • 1
    Single looses precision. Double should be a more appropriate candidate. – Maxime Labelle Jan 07 '13 at 12:52
  • 1
    However I used always double due to precision minor issues. It's correct Single is the exact one but I had some problems with that type. – Maximiliano Rios May 09 '13 at 13:29
  • This is an old question, but as a fun fact the EF 6.0 Model-First generator generates a SQL column of type `real` when you select `Single` as the .NET type – Matt Baker Dec 03 '14 at 18:40
  • Visual Studio 2105 wants to simplify 'Single' into 'float' – barrypicker Mar 29 '18 at 19:29
  • It is implementation-dependent (ie you need to check with each SQL version). The implementation in SQL Server 2012+ is IEEE 754 compliant except for lack of support for constants NaN and +/- Infinity. If all you want is to be able to store a massively large number and do not mind all the rounding and precision baggage that the double precision data type (officially declared as DOUBLE PRECISION, FLOAT(53), or just FLOAT) brings with it then you should be OK. – VoteCoffee May 16 '18 at 22:51
  • 1
    I tried using Single, but ReSharper suggested using float instead. – Rich Oct 18 '18 at 21:26
  • 2
    @Rich `float` *is* `Single`. Same as `int` being `Int32` or `long` being `Int64`, it's just a shorthand alias. – IAmJersh May 04 '21 at 13:33
10

Single is not the correct answer as it rounds the decimal part.. For instance 2.0799999 will be converted to 2.08. If there are no constraints regarding the rounding then it should be good.

msbyuva
  • 3,467
  • 13
  • 63
  • 87
7

Double can be used as the .NET Equivalent Datatype for Real Datatype of SQL Server

Double gets the exact value with out Rounding

msbyuva
  • 3,467
  • 13
  • 63
  • 87
  • The correct type for double data type in .NET Framework is float, you probably may get an excepction in execution time as I'm having at this moment. – Leonardo Jul 15 '16 at 21:41
  • @Leonardo - a C# struct 'System.Double' is the same as the intrinsic type 'double', not float as your comment suggests. – barrypicker Mar 29 '18 at 19:32
5

The answer is Single. If you use double and your SQL field is type real it will error out. I tested this myself and confirmed.

Guzumba
  • 101
  • 1
  • 6
3

Its Equivalent is Single. Single is A floating point number within the range of -3.40E +38 through 3.40E +38.

Here is the latest from MSDN describes all SqlDbType and C# Equivalents

Aman G
  • 61
  • 10
1

the answer is Single or float. (depending on style.) this is like the difference between String and string [source: ReSharper code suggestion "use type keyword" when using Single. it suggested using float.

1

in my project (acces -> firebird and ms sql -> c#) is real defined like single precission float point number...so I used float and everything is OK

prisznitz
  • 11
  • 1