Dela via


SqlErrorCollection Class

Definition

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

public ref class SqlErrorCollection sealed : System::Collections::ICollection
[System.ComponentModel.ListBindable(false)]
[System.Serializable]
public sealed class SqlErrorCollection : System.Collections.ICollection
[System.ComponentModel.ListBindable(false)]
public sealed class SqlErrorCollection : System.Collections.ICollection
public sealed class SqlErrorCollection : System.Collections.ICollection
[<System.ComponentModel.ListBindable(false)>]
[<System.Serializable>]
type SqlErrorCollection = class
    interface ICollection
    interface IEnumerable
[<System.ComponentModel.ListBindable(false)>]
type SqlErrorCollection = class
    interface ICollection
    interface IEnumerable
type SqlErrorCollection = class
    interface ICollection
    interface IEnumerable
Public NotInheritable Class SqlErrorCollection
Implements ICollection
Inheritance
SqlErrorCollection
Attributes
Implements

Examples

The following example displays each SqlError within the SqlErrorCollection collection.

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

Remarks

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

Properties

Count

Gets the number of errors in the collection.

Item[Int32]

Gets the error at the specified index.

Methods

CopyTo(Array, Int32)

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

CopyTo(SqlError[], Int32)

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

GetEnumerator()

Returns an enumerator that iterates through the SqlErrorCollection.

Explicit Interface Implementations

ICollection.IsSynchronized

For a description of this member, see IsSynchronized.

ICollection.SyncRoot

For a description of this member, see SyncRoot.

Applies to

See also