SqlParameter.Precision 屬性

定義

取得或設定用來表示 Value 屬性的最大位數。

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

屬性值

用來表示 Value 屬性的最大位數。 預設值為 0。 這表示資料提供者會設定 Value 的精確度。

實作

屬性

範例

下列範例會建立 並 SqlParameter 設定其部分屬性。

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

備註

屬性 Precision 是由 具有 SqlDbTypeDecimal 參數使用。

您不需要為輸入參數指定 和 Scale 屬性的值 Precision ,因為它們可以從參數值推斷。 PrecisionScale 是輸出參數的必要專案,以及您需要指定參數的完整中繼資料而不指出值的情況,例如指定具有特定有效位數和小數位數的 Null 值。

注意

不支援使用這個屬性來強制傳遞至資料庫的資料。 若要在將資料傳遞至資料庫之前四捨五入、截斷或強制資料,請在將值指派給參數 Value 的 屬性之前,先使用 Math 屬於命名空間的 System 類別。

注意

Microsoft .NET Framework.NET Framework 1.0 版隨附的資料提供者不會驗證 PrecisionScaleDecimal 參數值。 這可能會導致在資料來源插入截斷的資料。 如果您使用 .NET Framework 1.0 版,請先驗證 PrecisionScaleDecimal 的值,再設定參數值。 當您使用 .NET Framework 1.1 版或更新版本時,如果 Decimal 參數值設定無效 Precision ,則會擲回例外狀況。 Scale 超過 Decimal 參數小數位數的值仍會截斷。

適用於

另請參閱