SqlException.Errors Propriedade
Definição
Obtém uma coleção de um ou mais objetos SqlError que fornecem informações detalhadas sobre as exceções geradas pelo Provedor de Dados .NET Framework para SQL Server.Gets a collection of one or more SqlError objects that give detailed information about exceptions generated by the .NET Framework Data Provider for SQL Server.
public:
property System::Data::SqlClient::SqlErrorCollection ^ Errors { System::Data::SqlClient::SqlErrorCollection ^ get(); };
public System.Data.SqlClient.SqlErrorCollection Errors { get; }
member this.Errors : System.Data.SqlClient.SqlErrorCollection
Public ReadOnly Property Errors As SqlErrorCollection
Valor da propriedade
As instâncias coletadas da classe SqlError.The collected instances of the SqlError class.
Exemplos
O exemplo a seguir exibe cada SqlError uma dentro da SqlErrorCollection coleção.The 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
Comentários
A SqlErrorCollection classe sempre contém pelo menos uma instância da SqlError classe.The SqlErrorCollection class always contains at least one instance of the SqlError class.
Este é um wrapper para SqlErrorCollection .This is a wrapper for SqlErrorCollection. Para obter mais informações sobre erros do mecanismo de SQL Server, consulte mecanismo de banco de dados eventos e erros.For more information on SQL Server engine errors, see Database Engine Events and Errors.