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

プロパティ値

Int32

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

実装

属性

注釈

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

注意

プロパティは CommandTimeout 、古い APM (非同期プログラミング モデル) などの BeginExecuteReader非同期メソッド呼び出しによって無視されます。 これは、次のような 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);  
         }  
      }  
   }  
}  

適用対象

こちらもご覧ください