SqlConnection.ConnectionTimeout SqlConnection.ConnectionTimeout SqlConnection.ConnectionTimeout SqlConnection.ConnectionTimeout Property

定義

試行を中断してエラーを生成する前に、接続の確立時に待機する時間を取得します。Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.

public:
 property int ConnectionTimeout { int get(); };
[System.Data.DataSysDescription("SqlConnection_ConnectionTimeout")]
public int ConnectionTimeout { get; }
member this.ConnectionTimeout : int
Public ReadOnly Property ConnectionTimeout As Integer

プロパティ値

接続が開くのを待機する時間 (秒単位)。The time (in seconds) to wait for a connection to open. 既定値は 15 秒です。The default value is 15 seconds.

実装

例外

0 より小さい値が設定されました。The value set is less than 0.

次の例ではSqlConnection 、を作成Connection Timeoutし、接続文字列でを30秒に設定します。The following example creates a SqlConnection and sets the Connection Timeout to 30 seconds in the connection string. このコードによって接続が開かConnectionTimeoutれ、コンソールウィンドウにプロパティが表示されます。The code opens the connection and displays the ConnectionTimeout property in the console window.

private static void OpenSqlConnection()
{
    string connectionString = GetConnectionString();
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("State: {0}", connection.State);
        Console.WriteLine("ConnectionTimeout: {0}",
            connection.ConnectionTimeout);
    }
}

static private string GetConnectionString()
{
    // To avoid storing the connection string in your code, 
    // you can retrieve it from a configuration file, using the 
    // System.Configuration.ConfigurationSettings.AppSettings property 
    return "Data Source=(local);Initial Catalog=AdventureWorks;"
        + "Integrated Security=SSPI;Connection Timeout=30";
}
Private Sub OpenSqlConnection()
    Dim connectionString As String = GetConnectionString()
    Using connection As New SqlConnection(connectionString)
        connection.Open()

        Console.WriteLine("State: {0}", connection.State)
        Console.WriteLine("ConnectionTimeout: {0}", connection.ConnectionTimeout)
    End Using
End Sub

Private Function GetConnectionString() As String
    ' To avoid storing the connection string in your code,  
    ' you can retrieve it from a configuration file, using the
    ' System.Configuration.ConfigurationSettings.AppSettings property
    Return "Data Source=(local);Database=AdventureWorks;" _
      & "Integrated Security=SSPI;Connection Timeout=30;"
End Function

注釈

接続文字列内でConnect TimeoutまたはConnection Timeoutキーワードを使用して、接続がタイムアウトするまで待機する時間を設定できます。You can set the amount of time a connection waits to time out by using the Connect Timeout or Connection Timeout keywords in the connection string. 値0は制限がないことを示します。では、 ConnectionString接続の試行が無制限に待機するため、では回避する必要があります。A value of 0 indicates no limit, and should be avoided in a ConnectionString because an attempt to connect waits indefinitely.

適用対象

こちらもご覧ください