SqlError Classe
Definição
Coleta informações relevantes para um aviso ou erro retornado pelo SQL Server.Collects information relevant to a warning or error returned by SQL Server.
public ref class SqlError sealed
public sealed class SqlError
[System.Serializable]
public sealed class SqlError
type SqlError = class
[<System.Serializable>]
type SqlError = class
Public NotInheritable Class SqlError
- Herança
-
SqlError
- Atributos
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
Essa classe é criada pelo Provedor de Dados de .NET Framework para SQL Server quando ocorre um erro.This class is created by the .NET Framework Data Provider for SQL Server when an error occurs. Uma instância do SqlError é criada e gerenciada pelo SqlErrorCollection , que, por sua vez, é criada pela SqlException classe.An instance of SqlError is created and managed by the SqlErrorCollection, which in turn is created by the SqlException class.
As mensagens com nível de severidade 10 ou inferior são informativas e indicam problemas causados por erros nas informações inseridas por um usuário.Messages with a severity level of 10 or less are informational and indicate problems caused by mistakes in information that a user has entered. Os níveis de severidade de 11 a 16 são gerados pelo usuário e podem ser corrigidos pelo usuário.Severity levels from 11 through 16 are generated by the user, and can be corrected by the user. Os níveis de severidade de 17 a 25 indicam erros de software ou hardware.Severity levels from 17 through 25 indicate software or hardware errors. Quando ocorre um erro de nível 17, 18 ou 19, você pode continuar trabalhando, embora talvez não seja possível executar uma determinada instrução.When a level 17, 18, or 19 error occurs, you can continue working, although you might not be able to execute a particular statement.
O SqlConnection permanece aberto quando o nível de severidade é 19 ou inferior.The SqlConnection remains open when the severity level is 19 or less. Quando o nível de severidade é 20 ou superior, o servidor geralmente encerra o SqlConnection.When the severity level is 20 or greater, the server usually closes the SqlConnection. No entanto, o usuário pode reabrir a conexão e continuar.However, the user can reopen the connection and continue. Em ambos os casos, SqlException é gerada pelo método que executa o comando.In both cases, a SqlException is generated by the method executing the command.
Para obter mais informações sobre erros gerados pelo SQL Server, consulte mecanismo de banco de dados erros.For more information about errors generated by SQL Server, see Database Engine Errors. Para obter mais informações sobre os níveis de severidade, consulte mecanismo de banco de dados severidades de erro.For more information about severity levels, see Database Engine Error Severities.
Propriedades
Class |
Obtém o nível de gravidade do erro retornado do SQL Server.Gets the severity level of the error returned from SQL Server. |
LineNumber |
Obtém o número de linha dentro do lote de comandos Transact-SQL ou procedimento armazenado que gerou o erro.Gets the line number within the Transact-SQL command batch or stored procedure that contains the error. |
Message |
Obtém o texto que descreve o erro.Gets the text describing the error. |
Number |
Obtém um número que identifica o tipo de erro.Gets a number that identifies the type of error. |
Procedure |
Obtém o nome do procedimento armazenado ou da RPC (chamada de procedimento remoto) que gerou o erro.Gets the name of the stored procedure or remote procedure call (RPC) that generated the error. |
Server |
Obtém o nome da instância do SQL Server que gerou o erro.Gets the name of the instance of SQL Server that generated the error. |
Source |
Obtém o nome do provedor que gerou o erro.Gets the name of the provider that generated the error. |
State |
Obtém o código de estado exclusivo para o erro.Gets the unique state code for the error. |
Métodos
Equals(Object) |
Determina se o objeto especificado é igual ao objeto atual.Determines whether the specified object is equal to the current object. (Herdado de Object) |
GetHashCode() |
Serve como a função de hash padrão.Serves as the default hash function. (Herdado de Object) |
GetType() |
Obtém o Type da instância atual.Gets the Type of the current instance. (Herdado de Object) |
MemberwiseClone() |
Cria uma cópia superficial do Object atual.Creates a shallow copy of the current Object. (Herdado de Object) |
ToString() |
Obtém ou define o texto completo da mensagem de erro.Gets the complete text of the error message. |