SqlException.Errors 属性

定义

获取由一个或多个 SqlError 对象组成的集合,这些对象提供有关 SQL Server .NET Framework 数据提供程序所生成的异常的详细信息。

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 类的收集的实例。

示例

以下示例显示 SqlError 集合中的每个 SqlErrorCollection

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引擎错误的详细信息,请参阅数据库引擎事件和错误

适用于

另请参阅