Share via


SqlException.State プロパティ

定義

エラー、警告、または "データがありません" というメッセージを表す、数字によるエラー コードを SQL Server から取得します。 これらの値のデコード方法について詳しくは、データベース エンジンのイベントとエラーに関するページをご覧ください。

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

プロパティ値

エラー コードを表す数字。

次の例では、 コレクション内の各 SqlErrorSqlErrorCollection 表示します。

using Microsoft.Data.SqlClient;
using System.Text;

class Program
{
    static void Main()
    {
        string s = GetConnectionString();
        ShowSqlException(s);
        Console.ReadLine();
    }

    public static void ShowSqlException(string connectionString)
    {
        string queryString = "EXECUTE NonExistantStoredProcedure";
        StringBuilder errorMessages = new StringBuilder();

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            try
            {
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    errorMessages.Append("Index #" + i + "\n" +
                        "Message: " + ex.Errors[i].Message + "\n" +
                        "Error Number: " + ex.Errors[i].Number + "\n" +
                        "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                        "Source: " + ex.Errors[i].Source + "\n" +
                        "Procedure: " + ex.Errors[i].Procedure + "\n");
                }
                Console.WriteLine(errorMessages.ToString());
            }
        }
    }

    static private string GetConnectionString()
    {
        // To avoid storing the connection string in your code, 
        // you can retrieve it from a configuration file.
        return "Data Source=(local);Initial Catalog=AdventureWorks;"
            + "Integrated Security=SSPI";
    }
}

注釈

これは、 プロパティの State 最初 SqlError の の プロパティの Errors ラッパーです。

が の場合Errors、 のdefaultbyteが返nullされます。

適用対象

こちらもご覧ください