SqlErrorCollection 類別

定義

收集 .NET Framework Data Provider for SQL Server 所產生的所有錯誤。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
type SqlErrorCollection = class
    interface ICollection
    interface IEnumerable
Public NotInheritable Class SqlErrorCollection
Implements ICollection
繼承
SqlErrorCollection
屬性
實作

範例

下列範例會顯示 SqlErrorCollection 集合中的每個 SqlErrorThe 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();
}
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

備註

這個類別是由 SqlException 所建立,用以收集 SqlError 類別的實例。This class is created by SqlException to collect instances of the SqlError class. SqlErrorCollection 一律包含 SqlError 類別的至少一個實例。SqlErrorCollection always contains at least one instance of the SqlError class.

屬性

Count

取得集合中的錯誤數目。Gets the number of errors in the collection.

Item[Int32]

取得在指定之索引處的錯誤。Gets the error at the specified index.

方法

CopyTo(Array, Int32)

從指定的索引開始,將 SqlErrorCollection 集合的項目複製到 ArrayCopies the elements of the SqlErrorCollection collection into an Array, starting at the specified index.

CopyTo(SqlError[], Int32)

從指定的索引開始,將 SqlErrorCollection 集合的項目複製到 SqlErrorCollectionCopies the elements of the SqlErrorCollection collection into a SqlErrorCollection, starting at the specified index.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetEnumerator()

傳回逐一查看 SqlErrorCollection 的列舉值。Returns an enumerator that iterates through the SqlErrorCollection.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

ICollection.IsSynchronized

如需這個成員的說明,請參閱 IsSynchronizedFor a description of this member, see IsSynchronized.

ICollection.SyncRoot

如需這個成員的說明,請參閱 SyncRootFor a description of this member, see SyncRoot.

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱