SqlParameter.Scale 属性
定义
public:
property System::Byte Scale { System::Byte get(); void set(System::Byte value); };
public byte Scale { get; set; }
[System.Data.DataSysDescription("DbDataParameter_Scale")]
public byte Scale { get; set; }
member this.Scale : byte with get, set
[<System.Data.DataSysDescription("DbDataParameter_Scale")>]
member this.Scale : byte with get, set
Public Property Scale As Byte
属性值
对 Value 进行解析的小数位数。The number of decimal places to which Value is resolved. 默认值为 0。The default is 0.
实现
- 属性
示例
下面的示例创建 SqlParameter 并设置其一些属性。The following example creates a SqlParameter and sets some of its properties.
static void CreateSqlParameterPrecisionScale()
{
SqlParameter parameter = new SqlParameter("Price", SqlDbType.Decimal);
parameter.Value = 3.1416;
parameter.Precision = 8;
parameter.Scale = 4;
}
Private Sub CreateSqlParameterPrecisionScale()
Dim parameter As New SqlParameter("Price", SqlDbType.Decimal)
parameter.Value = 3.1416
parameter.Precision = 8
parameter.Scale = 4
End Sub
注解
Scale属性由具有、、或的参数使用 SqlDbType Decimal DateTime2 DateTimeOffset Time 。The Scale property is used by parameters that have a SqlDbType of Decimal, DateTime2, DateTimeOffset, or Time.
警告
如果 Scale 未显式指定此属性,并且服务器上的数据不适合 scale 0 (默认) ,则可能会截断数据。Data may be truncated if the Scale property is not explicitly specified and the data on the server does not fit in scale 0 (the default).
对于 DateTime2 类型,scale 0 (默认) 将作为 datetime2 (7) 传递。For the DateTime2 type, scale 0 (the default) will be passed as datetime2(7). 目前没有办法将参数作为 datetime2 发送 (0) 。There is currently no way to send a parameter as datetime2(0). 缩放1-7 按预期方式工作。Scales 1-7 work as expected.
此问题也适用于 DateTimeOffset 和 Time 。This problem applies to DateTimeOffset and Time as well.
不需要指定 Precision 输入参数的和属性的值 Scale ,因为可以从参数值推断它们。You do not need to specify values for the Precision and Scale properties for input parameters, as they can be inferred from the parameter value. Precision 对于 Scale 输出参数以及需要在不指示值的情况下指定参数的完整元数据(例如,用特定的精度和小数位数指定 null 值)的情况下,和是必需的。Precision and Scale are required for output parameters and for scenarios where you need to specify complete metadata for a parameter without indicating a value, such as specifying a null value with a specific precision and scale.
备注
不支持使用此属性来强制传递到数据库的数据。Use of this property to coerce data passed to the database is not supported. 若要在将数据传递到数据库之前对其进行舍入、截断或强制转换,请使用命名空间中的类,然后将 Math System 值分配给该参数的 Value 属性。To round, truncate, or otherwise coerce data before passing it to the database, use the Math class that is part of the System namespace prior to assigning a value to the parameter's Value property.
备注
.NET Framework 版本1.0 附带 .NET Framework 数据提供程序不验证 Precision Scale Decimal 参数值的或。.NET Framework data providers that are included with the .NET Framework version 1.0 do not verify the Precision or Scale of Decimal parameter values. 这可能会导致在数据源中插入截断的数据。This can cause truncated data to be inserted at the data source. 如果使用 .NET Framework 版本1.0,请 Precision SqlParameter Decimal 在设置参数值之前验证和值。If you are using .NET Framework version 1.0, validate the Precision and SqlParameter of Decimal values before setting the parameter value. Scale 超出 Decimal 参数范围的值仍会被截断。Scale values that exceed the Decimal parameter scale are still truncated.