SqlError Class

Definition

收集与 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
Public NotInheritable Class SqlError
Inheritance
SqlError
Attributes

Examples

下面的示例显示 SqlErrorCollection 集合中的每个 SqlErrorThe 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

Remarks

此类由 .NET Framework 数据提供程序在发生错误时由 SQL Server 创建。This class is created by the .NET Framework Data Provider for SQL Server when an error occurs. SqlError 的实例由 SqlErrorCollection创建和管理,后者又由 SqlException 类创建。An instance of SqlError is created and managed by the SqlErrorCollection, which in turn is created by the SqlException class.

严重级别为 10 或以下的消息为信息性的,并指示问题是由用户输入的信息中的错误导致的。Messages with a severity level of 10 or less are informational and indicate problems caused by mistakes in information that a user has entered. 严重级别为11到16的是用户生成的,并可由用户更正。Severity levels from 11 through 16 are generated by the user, and can be corrected by the user. 严重级别为 17 到 25 的消息指示存在软件或硬件错误。Severity levels from 17 through 25 indicate software or hardware errors. 如果发生级别为17、18或19的错误,您可以继续工作,不过您可能无法执行特定语句。When a level 17, 18, or 19 error occurs, you can continue working, although you might not be able to execute a particular statement.

当严重级别为 19 或以下时,SqlConnection 将保持打开状态。The SqlConnection remains open when the severity level is 19 or less. 当严重度等于或大于 20 时,服务器通常会关闭 SqlConnectionWhen the severity level is 20 or greater, the server usually closes the SqlConnection. 但是,用户可以重新打开连接并继续操作。However, the user can reopen the connection and continue. 最后两种情况下,执行该命令的方法将生成 SqlExceptionIn both cases, a SqlException is generated by the method executing the command.

有关 SQL Server 生成的错误的详细信息,请参阅数据库引擎错误的原因和解决方法For more information on errors generated by SQL Server, see Cause and Resolution of Database Engine Errors. 有关严重级别的详细信息,请参阅数据库引擎错误严重性For more information about severity levels, see Database Engine Error Severities.

Properties

Class

获取从 SQL Server 返回的错误的严重级别。Gets the severity level of the error returned from SQL Server.

LineNumber

从包含错误的 Transact-SQL 批命令或存储过程中获取行号。Gets the line number within the Transact-SQL command batch or stored procedure that contains the error.

Message

获取对错误进行描述的文本。Gets the text describing the error.

Number

获取一个标识错误类型的数字。Gets a number that identifies the type of error.

Procedure

获取生成错误的存储过程或远程过程调用 (RPC) 的名称。Gets the name of the stored procedure or remote procedure call (RPC) that generated the error.

Server

获取生成错误的 SQL Server 实例的名称。Gets the name of the instance of SQL Server that generated the error.

Source

获取生成错误的提供程序的名称。Gets the name of the provider that generated the error.

State

某些错误消息可能在数据库引擎的代码中多次引发。Some error messages can be raised at multiple points in the code for the Database Engine. 例如,几种不同的情况下可引发 1105 错误。For example, an 1105 error can be raised for several different conditions. 引发错误的每种特定情况将分配一个唯一状态代码。Each specific condition that raises an error assigns a unique state code.

Methods

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

获取错误信息的完整文本。Gets the complete text of the error message.

Applies to

See also