SqlCommand.CommandTimeout プロパティ

定義

コマンド実行の試行を終了してエラーを生成するまでの待機時間 (秒単位) を取得または設定します。

public:
 virtual property int CommandTimeout { int get(); void set(int value); };
public:
 property int CommandTimeout { int get(); void set(int value); };
public override int CommandTimeout { get; set; }
[System.Data.DataSysDescription("DbCommand_CommandTimeout")]
public int CommandTimeout { get; set; }
member this.CommandTimeout : int with get, set
[<System.Data.DataSysDescription("DbCommand_CommandTimeout")>]
member this.CommandTimeout : int with get, set
Public Overrides Property CommandTimeout As Integer
Public Property CommandTimeout As Integer

プロパティ値

コマンドの実行を待機する時間 (秒単位)。 既定値は 30 秒です。

実装

属性

注釈

値 0 は制限がないことを示します (コマンドを実行しようとすると無期限に待機します)。

注意

プロパティは CommandTimeout 、 などの BeginExecuteReader古い APM (非同期プログラミング モデル) 非同期メソッド呼び出しでは無視されます。 これは、 などの ExecuteReaderAsync新しい TAP (タスク非同期プログラミング) メソッドによって受け入れられます。

CommandTimeoutコマンドがコンテキスト接続 (SqlConnection接続文字列で "context connection=true" で開かれた) に対して実行された場合、影響はありません。

注意

このプロパティは、コマンドの実行中または結果の処理中のすべてのネットワーク読み取りに対する累積タイムアウト (メソッドの呼び出し中に読み取られるすべてのネットワーク パケットの場合) です。 最初の行が返された後もタイムアウトが発生する可能性があり、ユーザーの処理時間は含まず、ネットワーク読み取り時間のみが含まれます。

たとえば、30 秒のタイムアウトでは、2 つのネットワーク パケットが必要な場合 Read 、両方のネットワーク パケットを読み取るために 30 秒が必要になります。 もう一度を呼び出 Read すと、必要なデータを読み取るためにさらに 30 秒がかかります。

using System;
using System.Data.SqlClient;
///
public class A {
   ///
   public static void Main() {
      string connectionString = "";
      // Wait for 5 second delay in the command
      string queryString = "waitfor delay '00:00:05'";
      using (SqlConnection connection = new SqlConnection(connectionString)) {
         connection.Open();
         SqlCommand command = new SqlCommand(queryString, connection);
         // Setting command timeout to 1 second
         command.CommandTimeout = 1;
         try {
            command.ExecuteNonQuery();
         }
         catch (SqlException e) {
            Console.WriteLine("Got expected SqlException due to command timeout ");
            Console.WriteLine(e);
         }
      }
   }
}

適用対象

こちらもご覧ください