在数据库中浮动到 ? 在.NET中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/546150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Float in Database to ? in .NET
提问by Lieven Cardoen
If you have a float in MSSQLServer, to what do you map this in .NET?
如果您在 MSSQLServer 中有一个浮点数,您在 .NET 中将它映射到什么?
Can you convert it to Double or will you lose numbers?
你能把它转换成 Double 还是会丢失数字?
回答by tvanfosson
SQLServer floatand C#/VB doublehave the same representation. This is the correct mapping. What you don't want to do is map SQL Server floatto C#/VB floatas that may involve a loss of precision. SQL Server realmaps onto C#/VB float.
SQLServerfloat和 C#/VBdouble具有相同的表示形式。这是正确的映射。您不想做的是将 SQL Server 映射float到 C#/VB,float因为这可能会导致精度损失。SQL Serverreal映射到 C#/VB float。
T-SQL float and real type definitions can be found at MSDN. C# double definition can be found at MSDNas well, as can the float definition.
T-SQL float 和 real 类型定义可以在MSDN上找到。C# 双重定义也可以在MSDN上找到,浮点定义也可以。
回答by kristof
Check out:
查看:
- SQL Server Data Types and Their .NET Framework Equivalents (msdn)for full reference on data type mappings between SQL Server and .Net
- SQL Server 数据类型及其 .NET Framework 等效项 (msdn),以提供有关 SQL Server 和 .Net 之间数据类型映射的完整参考
In your case SQL Server native type floatmaps to SQL Server CLR SqlDoubleand then Doublein .Net
在您的情况下,SQL Server 本机类型浮点数映射到 SQL Server CLR SqlDouble,然后在 .Net 中使用Double
Update (May 2016):
更新(2016 年 5 月):
Updated version of the msdn document: Mapping CRL Parameter Data
msdn 文档的更新版本:映射 CRL 参数数据
回答by Joel Coehoorn
C# has a float type, but it will convert to double just fine as well.
C# 有一个 float 类型,但它也可以很好地转换为 double 类型。
回答by flatline
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.
根据我的记忆,大多数 ORM 工具都会将其映射为 Decimal 类型,不会像 double 或 float 那样丢失精度。
回答by Misko
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.
这取决于 SQL 浮点数的大小。对于等同于 float(53) 的普通“float”,您需要使用 C# double。对于 float(24) 或更低的 C# float 就足够了,或者 double 也可以。
回答by Avram
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.
Microsoft XSD 生成的自动代码将 SQL 浮点数(默认浮点数)转换为 C# 双精度数。
所以你可以建议这不是大错误。
直到您使用具有默认大小的浮点数。

