Share via


SqlException.Errors Eigenschaft

Definition

Ruft eine Auflistung von mindestens einem SqlError-Objekt ab, die ausführliche Informationen über die vom .NET Framework-Datenanbieter für SQL Server generierte Ausnahmen enthält.

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

Eigenschaftswert

Die gesammelten Instanzen der SqlError-Klasse.

Beispiele

Im folgenden Beispiel werden alle SqlError innerhalb der SqlErrorCollection Auflistung angezeigt.

using Microsoft.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";

        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();
    }

    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";
    }
}

Hinweise

Die SqlErrorCollection -Klasse enthält immer mindestens eine Instanz der SqlError -Klasse.

Dies ist ein Wrapper für SqlErrorCollection. Weitere Informationen zu SQL Server Engine-Fehlern finden Sie unter Ereignisse und Fehler der Datenbank-Engine.

Gilt für:

Weitere Informationen