SqlConnection.ConnectionTimeout 속성

정의

시도를 종료하고 오류를 생성하기 전에 연결하는 동안 대기할 시간(초 단위)을 가져옵니다.Gets the time to wait (in seconds) while trying to establish a connection before terminating the attempt and generating an error.

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

속성 값

Int32

연결이 열리기 전까지 대기할 시간(초)입니다.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.

적용 대상