Share via


SqlErrorCollection 类

定义

收集用于 SQL Server 的 .NET Framework 数据提供程序生成的所有错误。 此类不能被继承。

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
继承
SqlErrorCollection
属性
实现

示例

以下示例显示 SqlError 集合中的每个 SqlErrorCollection

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

注解

创建此类用于 SqlException 收集 类的 SqlError 实例。 SqlErrorCollection 始终至少包含类的 SqlError 一个实例。

属性

Count

获取集合中错误的数目。

Item[Int32]

获取位于指定索引处的错误。

方法

CopyTo(Array, Int32)

SqlErrorCollection 集合的元素复制到 Array 中,从指定索引开始。

CopyTo(SqlError[], Int32)

SqlErrorCollection 集合的元素复制到 SqlErrorCollection 中,从指定索引开始。

GetEnumerator()

返回循环访问 SqlErrorCollection 的枚举数。

显式接口实现

ICollection.IsSynchronized

有关此成员的说明,请参见 IsSynchronized

ICollection.SyncRoot

有关此成员的说明,请参见 SyncRoot

适用于

另请参阅