SqlError.Class 屬性

定義

會取得 SQL Server 回傳錯誤的嚴重程度。

public:
 property System::Byte Class { System::Byte get(); };
public byte Class { get; }
member this.Class : byte
Public ReadOnly Property Class As Byte

屬性值

從1到25的數值表示錯誤的嚴重程度。 預設值是 0。

範例

以下範例顯示每個集合中的SqlError每個角色SqlErrorCollection

public void DisplaySqlErrors(SqlException exception)
{
    for (int i = 0; i < exception.Errors.Count; i++)
    {
        Console.WriteLine("Index #" + i + "\n" +
            "Source: " + exception.Errors[i].Source + "\n" +
            "Number: " + exception.Errors[i].Number.ToString() + "\n" +
            "State: " + exception.Errors[i].State.ToString() + "\n" +
            "Class: " + exception.Errors[i].Class.ToString() + "\n" +
            "Server: " + exception.Errors[i].Server + "\n" +
            "Message: " + exception.Errors[i].Message + "\n" +
            "Procedure: " + exception.Errors[i].Procedure + "\n" +
            "LineNumber: " + exception.Errors[i].LineNumber.ToString());
    }
    Console.ReadLine();
}
Public Sub DisplaySqlErrors(ByVal exception As SqlException)
    Dim i As Integer

    For i = 0 To exception.Errors.Count - 1
        Console.WriteLine(("Index #" & i & ControlChars.NewLine & _
            "Source: " & exception.Errors(i).Source & ControlChars.NewLine & _
            "Number: " & exception.Errors(i).Number.ToString() & ControlChars.NewLine & _
            "State: " & exception.Errors(i).State.ToString() & ControlChars.NewLine & _
            "Class: " & exception.Errors(i).Class.ToString() & ControlChars.NewLine & _
            "Server: " & exception.Errors(i).Server & ControlChars.NewLine & _
            "Message: " & exception.Errors(i).Message & ControlChars.NewLine & _
            "Procedure: " & exception.Errors(i).Procedure & ControlChars.NewLine & _
            "LineNumber: " & exception.Errors(i).LineNumber.ToString()))
    Next i
    Console.ReadLine()
End Sub

備註

嚴重程度等級為 10 或以下的訊息具有資訊性,表示因使用者輸入資訊錯誤而產生的問題。 11到16的嚴重程度等級由使用者產生,並可由使用者自行修正。 17至25的嚴重程度表示軟體或硬體錯誤。 當發生第 17、18 或 19 級錯誤時,你可以繼續工作,但可能無法執行特定語句。

當嚴重程度低於19時,該系統 SqlConnection 仍保持開放。 當嚴重程度達到 20 或以上時,伺服器通常會關閉 SqlConnection。 不過,使用者可以重新開啟連線並繼續使用。 在這兩種情況下,a SqlException 都是由執行指令的方法產生。

欲了解更多由SQL Server產生錯誤的資訊,請參見 資料庫引擎 事件與錯誤

適用於

另請參閱