SqlError 類別

定義

收集與 SQL Server 所傳回之警告或錯誤相關的資訊。Collects information relevant to a warning or error returned by SQL Server.

public ref class SqlError sealed
[System.Serializable]
public sealed class SqlError
type SqlError = class
Public NotInheritable Class SqlError
繼承
SqlError
屬性

範例

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

public static void ShowSqlException(string connectionString)
{
    string queryString = "EXECUTE NonExistantStoredProcedure";
    
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        try
        {
            command.Connection.Open();
            command.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            DisplaySqlErrors(ex);
        }
    }
}

private static void DisplaySqlErrors(SqlException exception)
{
    for (int i = 0; i < exception.Errors.Count; i++)
    {
        Console.WriteLine("Index #" + i + "\n" +
            "Error: " + exception.Errors[i].ToString() + "\n");
    }
    Console.ReadLine();
}
Public Sub ShowSqlException(ByVal connectionString As String)
    Dim queryString As String = "EXECUTE NonExistantStoredProcedure"

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

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

        Catch ex As SqlException
            DisplaySqlErrors(ex)
        End Try
    End Using
End Sub

Private Sub DisplaySqlErrors(ByVal exception As SqlException)
    Dim i As Integer

    For i = 0 To exception.Errors.Count - 1
        Console.WriteLine("Index #" & i & ControlChars.NewLine & _
            "Error: " & exception.Errors(i).ToString() & ControlChars.NewLine)
    Next i
    Console.ReadLine()
End Sub

備註

此類別是在發生錯誤時,SQL Server 的 .NET Framework Data Provider 所建立。This class is created by the .NET Framework Data Provider for SQL Server when an error occurs. SqlError 的實例是由 SqlErrorCollection建立和管理,而後者則是由 SqlException 類別所建立。An instance of SqlError is created and managed by the SqlErrorCollection, which in turn is created by the SqlException class.

安全性層級 10 或以下的訊息是告知性的訊息,用以指出問題產生的原因是使用者輸入了錯誤的資訊。Messages with 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 usually 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 產生之錯誤的詳細資訊,請參閱資料庫引擎錯誤的原因和解決方式。For more information on errors generated by SQL Server, see Cause and Resolution of Database Engine Errors. 如需嚴重性層級的詳細資訊,請參閱資料庫引擎錯誤嚴重性For more information about severity levels, see Database Engine Error Severities.

屬性

Class

取得從 SQL Server 傳回之錯誤的嚴重性層級。Gets the severity level of the error returned from SQL Server.

LineNumber

取得包含錯誤之 Transact-SQL 命令批次或預存程序內的行號。Gets the line number within the Transact-SQL command batch or stored procedure that contains the error.

Message

取得描述錯誤的文字。Gets the text describing the error.

Number

取得識別錯誤類型的值。Gets a number that identifies the type of error.

Procedure

取得產生錯誤之預存程序或遠端程序呼叫 (RPC) 的名稱。Gets the name of the stored procedure or remote procedure call (RPC) that generated the error.

Server

取得產生錯誤之 SQL Server 執行個體的名稱。Gets the name of the instance of SQL Server that generated the error.

Source

取得產生錯誤之提供者的名稱。Gets the name of the provider that generated the error.

State

某些錯誤訊息可能會在資料庫引擎程式碼中的多個點上引發。Some error messages can be raised at multiple points in the code for the Database Engine. 例如,1105 錯誤可能會因為許多不同情況而引發。For example, an 1105 error can be raised for several different conditions. 每個引發錯誤的特定條件會指派唯一的狀態碼。Each specific condition that raises an error assigns a unique state code.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

取得錯誤訊息的完整文字。Gets the complete text of the error message.

適用於

另請參閱