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:
 virtual property int ConnectionTimeout { int get(); };
[System.Data.DataSysDescription("SqlConnection_ConnectionTimeout")]
public override int ConnectionTimeout { get; }
member this.ConnectionTimeout : int
Public Overrides 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.

適用対象

こちらもご覧ください