SqlErrorCollection.Item[Int32] 属性

定义

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

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

参数

index
Int32

要检索的错误的从零开始的索引。

属性值

一个 SqlError,包含指定索引位置的错误。

例外

索引参数超出数组界限。

示例

以下示例显示 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";
    }
}

适用于

另请参阅