Redigeeri

Jagamisviis:


SqlParameter.Precision Property

Definition

Gets or sets the maximum number of digits used to represent the Value property.

public:
 property System::Byte Precision { System::Byte get(); void set(System::Byte value); };
public byte Precision { get; set; }
[System.Data.DataSysDescription("DbDataParameter_Precision")]
public byte Precision { get; set; }
member this.Precision : byte with get, set
[<System.Data.DataSysDescription("DbDataParameter_Precision")>]
member this.Precision : byte with get, set
Public Property Precision As Byte

Property Value

The maximum number of digits used to represent the Value property. The default value is 0. This indicates that the data provider sets the precision for Value.

Implements

Attributes

Examples

The following example creates a SqlParameter and sets some of its properties.

private static void AddSqlParameter(SqlCommand command)
{
    SqlParameter parameter = new SqlParameter("@Price", SqlDbType.Decimal);
    parameter.Value = 3.1416;
    parameter.Precision = 8;
    parameter.Scale = 4;

    command.Parameters.Add(parameter);
}
Private Sub AddSqlParameter(ByVal command As SqlCommand)

    Dim parameter As New SqlParameter("@Price", SqlDbType.Decimal)
    With parameter
        .Value = 3.1416
        .Precision = 8
        .Scale = 4
    End With

    command.Parameters.Add(parameter)
End Sub

Remarks

The Precision property is used by parameters that have a SqlDbType of Decimal.

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 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.

Note

Use of this property to coerce data passed to the database is not supported. 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.

Note

Microsoft .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 being inserted at the data source. If you are using .NET Framework version 1.0, validate the Precision and Scale of Decimal values before setting the parameter value. When you use .NET Framework version 1.1 or a later version, an exception is thrown if a Decimal parameter value is set with an invalid Precision. Scale values that exceed the Decimal parameter scale are still truncated.

Applies to

See also