Bagikan melalui


SqlErrorCollection.Item[Int32] Properti

Definisi

Mendapatkan kesalahan pada indeks yang ditentukan.

public:
 property System::Data::SqlClient::SqlError ^ default[int] { System::Data::SqlClient::SqlError ^ get(int index); };
public System.Data.SqlClient.SqlError this[int index] { get; }
member this.Item(int) : System.Data.SqlClient.SqlError
Default Public ReadOnly Property Item(index As Integer) As SqlError

Parameter

index
Int32

Indeks berbasis nol dari kesalahan yang akan diambil.

Nilai Properti

yang SqlError berisi kesalahan pada indeks yang ditentukan.

Pengecualian

Parameter indeks berada di luar batas array.

Contoh

Contoh berikut menampilkan masing-masing SqlError dalam SqlErrorCollection koleksi.

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

Berlaku untuk

Lihat juga