語言

SqlParameter.Precision 屬性

定義

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

public:
 property System::Byte Precision { System::Byte get(); void set(System::Byte value); };
public byte Precision { get; set; }
member this.Precision : byte with get, set
Public Property Precision As Byte

屬性值

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

實作

範例

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

using Microsoft.Data.SqlClient;

class Program
{
    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);
    }
}

備註

Precision屬性是由 具有 SqlDbType 的參數 Decimal 所使用。

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

注意

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

注意

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

適用於