SqlError.State Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Some error messages can be raised at multiple points in the code for the Database Engine. For example, an 1105 error can be raised for several different conditions. Each specific condition that raises an error assigns a unique state code.
public:
property System::Byte State { System::Byte get(); };
public byte State { get; }
member this.State : byte
Public ReadOnly Property State As Byte
The state code.
The following example displays each SqlError within the SqlErrorCollection collection.
using Microsoft.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace Classic_WebData_SqlError.StateCS
{
class Program
{
static void Main()
{
//DisplaySqlErrors();
}
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();
}
}
}
State is only set for errors that are received from the server.
For more information on errors generated by SQL Server, see Understanding Database Engine Errors.
Product | Versions |
---|---|
SqlClient .NET Core | 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1, 5.0, 5.1, 5.2 |
SqlClient .NET Framework | 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1, 5.0, 5.1, 5.2 |
SqlClient .NET Standard | 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1, 5.0, 5.1, 5.2 |