SqlConnection.ConnectionTimeout Propriedade

Definição

Obtém o tempo de espera (em segundos) durante a tentativa de estabelecimento da conexão antes que a tentativa seja terminada e um erro seja gerado.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

Valor da propriedade

Int32

O tempo (em segundos) de espera para abrir uma conexão.The time (in seconds) to wait for a connection to open. O valor padrão é 15 segundos.The default value is 15 seconds.

Implementações

Atributos

Exceções

O valor definido é menor que 0.The value set is less than 0.

Exemplos

O exemplo a seguir cria um SqlConnection e define o Connection Timeout como 30 segundos na cadeia de conexão.The following example creates a SqlConnection and sets the Connection Timeout to 30 seconds in the connection string. O código abre a conexão e exibe a ConnectionTimeout Propriedade na janela do console.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

Comentários

Você pode definir a quantidade de tempo que uma conexão aguarda para atingir o tempo limite usando Connect Timeout as Connection Timeout palavras-chave ou na cadeia de conexão.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. Um valor de 0 indica que não há limite e deve ser evitado em um ConnectionString porque uma tentativa de conectar espera indefinidamente.A value of 0 indicates no limit, and should be avoided in a ConnectionString because an attempt to connect waits indefinitely.

Aplica-se a