验证在C#中的十进制存储在SQL Server中 [英] Validating decimal in C# for storage in SQL Server

查看:124
本文介绍了验证在C#中的十进制存储在SQL Server中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小数数据库列十进制(26,6)

I have a decimal database column decimal (26,6).

据我所知这意味着26位精度和6的比例

As far as I can gather this means a precision of 26 and a scale of 6.

我认为,这意味着该数量可以是一个总长度为26位数字,这些数字6可以是之后小数位。

I think this means that the number can be a total of 26 digits in length and 6 of these digits can be after the decimal place.

在我的WPF / C#前端我需要验证传入小数,这样我可以肯定,它可以存储在SQL Server不截断等

In my WPF / C# frontend I need to validate an incoming decimal so that I can be sure that it can be stored in SQL Server without truncation etc.

所以我的问题是有没有办法来检查小数具有特定精度和规模。

So my question is there a way to check that decimal has a particular precision and scale.

另外顺便说一句我听说,SQL Server将在一个完全不同的方式向CLR十进制,这是真的,如果是有什么事情我需要担心?

Also as an aside I have heard that SQL Server stores decimal in a completely different way to the CLR, is this true and if so is it something I need to worry about?

推荐答案

直接的方法来确定是否一个给定的精度,十进制数的规模大于26.6将检查对应的字符串的长度。

straight forward way to determine if a given precision,scale of decimal number is greater than 26,6 would be to check the length of its string equivalent.

    public static bool WillItTruncate(double dNumber, int precision, int scale) {
        string[] dString = dNumber.ToString("#.#", CultureInfo.InvariantCulture).Split('.');
        return (dString[0].Length > (precision - scale) || dString.Length>1?dString[1].Length > scale:true);
    }

有关C#十进制数据类型的最大精度似乎是29位,而SQL小数可以有38位。所以,你可能不打从C#SQL十进制的最大值。

The maximum precision for C# decimal datatype seems to be 29 digits whereas SQL decimal can have 38 digits. So you may not be hitting the maximum value of SQL decimal from C#.

这篇关于验证在C#中的十进制存储在SQL Server中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆