SqlErrorCollection Class

SqlErrorCollection Class

 

Collects all errors generated by the .NET Framework Data Provider for SQL Server. This class cannot be inherited.

Namespace:   System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)

System.Object
  System.Data.SqlClient.SqlErrorCollection

[SerializableAttribute]
[ListBindableAttribute(false)]
public sealed class SqlErrorCollection : ICollection, IEnumerable

NameDescription
System_CAPS_pubpropertyCount

Gets the number of errors in the collection.

System_CAPS_pubpropertyItem[Int32]

Gets the error at the specified index.

NameDescription
System_CAPS_pubmethodCopyTo(Array, Int32)

Copies the elements of the SqlErrorCollection collection into an Array, starting at the specified index.

System_CAPS_pubmethodCopyTo(SqlError[], Int32)

Copies the elements of the SqlErrorCollection collection into a SqlErrorCollection, starting at the specified index.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetEnumerator()

Returns an enumerator that iterates through the SqlErrorCollection.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.IsSynchronized

For a description of this member, see IsSynchronized.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.SyncRoot

For a description of this member, see SyncRoot.

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast<TResult>()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType<TResult>()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

This class is created by SqlException to collect instances of the SqlError class. SqlErrorCollection always contains at least one instance of the SqlError class.

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

.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft