共用方式為


SqlException.Errors 屬性

定義

取得一個或多個 SqlError 物件的集合,這些物件可提供有關 .NET Framework Data Provider for SQL Server 產生的例外狀況之詳細資訊。

public:
 property System::Data::SqlClient::SqlErrorCollection ^ Errors { System::Data::SqlClient::SqlErrorCollection ^ get(); };
public System.Data.SqlClient.SqlErrorCollection Errors { get; }
member this.Errors : System.Data.SqlClient.SqlErrorCollection
Public ReadOnly Property Errors As SqlErrorCollection

屬性值

SqlError 類別的已收集執行個體。

範例

下列範例會顯示集合內的每一個SqlErrorSqlErrorCollection

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

備註

類別 SqlErrorCollection 一律包含至少一個 類別的 SqlError 實例。

這是的包裝函式 SqlErrorCollection。 如需 SQL Server 引擎錯誤的詳細資訊,請參閱 Database Engine 事件和錯誤

適用於

另請參閱