SqlErrorCollection.Item[Int32] 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取位于指定索引处的错误。
public:
property System::Data::SqlClient::SqlError ^ default[int] { System::Data::SqlClient::SqlError ^ get(int index); };
public System.Data.SqlClient.SqlError this[int index] { get; }
member this.Item(int) : System.Data.SqlClient.SqlError
Default Public ReadOnly Property Item(index As Integer) As SqlError
参数
- index
- Int32
要检索的错误的从零开始的索引。
属性值
一个 SqlError,包含指定索引位置的错误。
例外
索引参数超出数组界限。
示例
以下示例显示 SqlError 集合中的每个 SqlErrorCollection 。
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