Float in Database to ? in .NET

前端 未结 6 2184
花落未央
花落未央 2020-12-30 20:06

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条回答
  • 2020-12-30 20:14

    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.

    0 讨论(0)
  • 2020-12-30 20:20

    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.

    0 讨论(0)
  • 2020-12-30 20:29

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

    0 讨论(0)
  • 2020-12-30 20:38

    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.

    0 讨论(0)
  • 2020-12-30 20:40

    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

    0 讨论(0)
  • 2020-12-30 20:41

    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.

    0 讨论(0)
提交回复
热议问题