SqlError.State 属性

定义

获取错误的唯一状态代码。

public:
 property System::Byte State { System::Byte get(); };
public byte State { get; }
member this.State : byte
Public ReadOnly Property State As Byte

属性值

状态代码。

示例

以下示例显示 SqlError 集合中的每个 SqlErrorCollection

public void DisplaySqlErrors(SqlException exception)
{
    for (int i = 0; i < exception.Errors.Count; i++)
    {
        Console.WriteLine("Index #" + i + "\n" +
            "Source: " + exception.Errors[i].Source + "\n" +
            "Number: " + exception.Errors[i].Number.ToString() + "\n" +
            "State: " + exception.Errors[i].State.ToString() + "\n" +
            "Class: " + exception.Errors[i].Class.ToString() + "\n" +
            "Server: " + exception.Errors[i].Server + "\n" +
            "Message: " + exception.Errors[i].Message + "\n" +
            "Procedure: " + exception.Errors[i].Procedure + "\n" +
            "LineNumber: " + exception.Errors[i].LineNumber.ToString());
    }
    Console.ReadLine();
}
Public Sub DisplaySqlErrors(ByVal exception As SqlException)
    Dim i As Integer

    For i = 0 To exception.Errors.Count - 1
        Console.WriteLine(("Index #" & i & ControlChars.NewLine & _
            "Source: " & exception.Errors(i).Source & ControlChars.NewLine & _
            "Number: " & exception.Errors(i).Number.ToString() & ControlChars.NewLine & _
            "State: " & exception.Errors(i).State.ToString() & ControlChars.NewLine & _
            "Class: " & exception.Errors(i).Class.ToString() & ControlChars.NewLine & _
            "Server: " & exception.Errors(i).Server & ControlChars.NewLine & _
            "Message: " & exception.Errors(i).Message & ControlChars.NewLine & _
            "Procedure: " & exception.Errors(i).Procedure & ControlChars.NewLine & _
            "LineNumber: " & exception.Errors(i).LineNumber.ToString()))
    Next i
    Console.ReadLine()
End Sub

注解

某些错误消息可能在 数据库引擎代码中多处出现。 例如,几种不同情况下都可能引发 1105 错误。 引发错误的每种特定情况将分配一个唯一状态代码。

仅针对从服务器收到的错误设置状态。

有关SQL Server生成的错误的详细信息,请参阅了解数据库引擎错误

适用于

另请参阅