接続タイムアウトエラーがスローされる方法を確認できる例はありますか?
50 000 行のように選択する選択クエリを配置して、GridView
. 接続文字列を設定し、接続を開いて設定しSystem.Threading.Thread.Sleep(30000)
ます。IIS 接続タイムアウトを 60 秒に設定しましたが、機能せず、エラーも発生しません。
私はSqlサーバーを使用しています
接続タイムアウトエラーがスローされる方法を確認できる例はありますか?
50 000 行のように選択する選択クエリを配置して、GridView
. 接続文字列を設定し、接続を開いて設定しSystem.Threading.Thread.Sleep(30000)
ます。IIS 接続タイムアウトを 60 秒に設定しましたが、機能せず、エラーも発生しません。
私はSqlサーバーを使用しています
SQL Server では、waitforコマンドを使用します。
waitfor delay '01:00'
接続文字列に設定できるはConnection Timeout
、接続が初期化されるまでクライアントが待機する時間を決定するために使用されます(適切な言葉がないため)。クライアントがデータベース操作の結果を待つ時間は制御しません。
そのタイムアウトを設定するには、次のCommandTimeout
プロパティを使用して実行できますSqlCommand
。
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["test"].ConnectionString))
using (SqlCommand comm = new SqlCommand("testTimeout", conn))
{
// next line is where the Connection Timeout will take
// effect if the connection cannot be opened in time
conn.Open();
comm.CommandType = System.Data.CommandType.StoredProcedure;
comm.CommandTimeout = 60;
// next line is where the Command Timeout takes effect
// if the operation takes a long time to complete
comm.ExecuteNonQuery();
}
コマンド タイムアウトをテストするには、「testTimeout」ストアド プロシージャを作成し、使用するコマンド タイムアウトよりも長く遅延させます ( Blorgbeardからの回答を参照)。コマンドのタイムアウトのデフォルト値は 30 (=30 秒) です。MSDNも参照してください。