SqlException.Class 屬性

定義

取得從 .NET Framework Data Provider for SQL Server 傳回的錯誤之嚴重性層級。Gets the severity level of the error returned from the .NET Framework Data Provider for 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 的值,表示錯誤的嚴重性層級。A value from 1 to 25 that indicates the severity level of the error.

範例

下列範例會顯示 SqlErrorCollection 集合中的每個 SqlErrorThe following example displays each SqlError within the SqlErrorCollection collection.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

class Program
{
    static void Main()
    {
        string s = GetConnectionString();
        ShowSqlException(s);
        Console.ReadLine();
    }

    public static void ShowSqlException(string connectionString)
    {
        string queryString = "EXECUTE NonExistantStoredProcedure";
        StringBuilder errorMessages = new StringBuilder();
        
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            try
            {
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    errorMessages.Append("Index #" + i + "\n" +
                        "Message: " + ex.Errors[i].Message + "\n" +
                        "Error Number: " + ex.Errors[i].Number + "\n" +
                        "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                        "Source: " + ex.Errors[i].Source + "\n" +
                        "Procedure: " + ex.Errors[i].Procedure + "\n");
                }
                Console.WriteLine(errorMessages.ToString());
            }
        }
    }

    static private string GetConnectionString()
    {
        // To avoid storing the connection string in your code, 
        // you can retrieve it from a configuration file.
        return "Data Source=(local);Initial Catalog=AdventureWorks;"
            + "Integrated Security=SSPI";
    }
}
Public Sub ShowSqlException(ByVal connectionString As String)
    Dim queryString As String = "EXECUTE NonExistantStoredProcedure"
    Dim errorMessages As New StringBuilder()

    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)

        Try
            command.Connection.Open()
            command.ExecuteNonQuery()

        Catch ex As SqlException
            Dim i As Integer
            For i = 0 To ex.Errors.Count - 1
                errorMessages.Append("Index #" & i.ToString() & ControlChars.NewLine _
                    & "Message: " & ex.Errors(i).Message & ControlChars.NewLine _
                    & "Error Number: " & ex.Errors(i).Number & ControlChars.NewLine _
                    & "LineNumber: " & ex.Errors(i).LineNumber & ControlChars.NewLine _
                    & "Source: " & ex.Errors(i).Source & ControlChars.NewLine _
                    & "Procedure: " & ex.Errors(i).Procedure & ControlChars.NewLine)
            Next i
            Console.WriteLine(errorMessages.ToString())
        End Try
    End Using
End Sub

備註

嚴重性層級為10或以下的訊息為資訊,並指出使用者已輸入的資訊中的錯誤所造成的問題。Messages that have a severity level of 10 or less are informational and indicate problems caused by mistakes in information that a user has entered. 從11到16的嚴重性層級是由使用者所產生,並可由使用者加以更正。Severity levels from 11 through 16 are generated by the user, and can be corrected by the user. 17 到 25 的嚴重性層級表示軟體或硬體錯誤。Severity levels from 17 through 25 indicate software or hardware errors. 當層級17、18或19發生錯誤時,您可以繼續工作,但您可能無法執行特定語句。When a level 17, 18, or 19 error occurs, you can continue working, although you might not be able to execute a particular statement.

當嚴重性層級為 19 或低於 19 時,SqlConnection 仍保持開啟。The SqlConnection remains open when the severity level is 19 or less. 當嚴重性層級為20或更大時,伺服器通常會關閉 SqlConnectionWhen the severity level is 20 or greater, the server ordinarily closes the SqlConnection. 但是,使用者可以再次開啟連線,然後繼續進行。However, the user can reopen the connection and continue. 在這兩個情況中,SqlException 皆由執行該命令的方法所產生。In both cases, a SqlException is generated by the method executing the command.

如需 SQL Server 所傳送之警告和參考用訊息的相關資訊,請參閱 SQL Server 檔的疑難排解一節。For information about the warning and informational messages sent by SQL Server, see the Troubleshooting section of the SQL Server documentation.

這是 Errors 屬性中第一個 SqlErrorClass 屬性的包裝函式。This is a wrapper for the Class property of the first SqlError in the Errors property.

適用於

另請參閱