.NET Framework Class Library
SqlConnection..::.ConnectionTimeout Property

Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.

Namespace:  System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)
Syntax

Visual Basic (Declaration)
Public Overrides ReadOnly Property ConnectionTimeout As Integer
Visual Basic (Usage)
Dim instance As SqlConnection
Dim value As Integer

value = instance.ConnectionTimeout
C#
public override int ConnectionTimeout { get; }
Visual C++
public:
virtual property int ConnectionTimeout {
    int get () override;
}
JScript
public override function get ConnectionTimeout () : int

Property Value

Type: System..::.Int32
The time (in seconds) to wait for a connection to open. The default value is 15 seconds.

Implements

IDbConnection..::.ConnectionTimeout
Exceptions

ExceptionCondition
ArgumentException

The value set is less than 0.

Remarks

You can set the amount of time a connection waits to time out by using the ConnectTimeout or Connection Timeout keywords in the connection string. A value of 0 indicates no limit, and should be avoided in a ConnectionString because an attempt to connect waits indefinitely.

Examples

The following example creates a SqlConnection and sets the Connection Timeout to 30 seconds in the connection string. The code opens the connection and displays the ConnectionTimeout property in the console window.

Visual Basic
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
C#
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";
}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
See Also

Reference

Other Resources

Tags :


Community Content

jZe
SqlCommand.CommandTimeout in Regular or Context Connections
Please note the SqlCommand.CommandTimeout dependence or independence of this ConnectionTimeout setting depending on the type of connection

Please view the important differences between Regular and Context Connections ("context connection=true" in the connection string)
  • Context Connection Described: http://msdn.microsoft.com/en-us/library/ms131053.aspx
  • Differences between Regular and Context Connection http://msdn.microsoft.com/en-us/library/ms131104.aspx
  • Restrictions : http://msdn.microsoft.com/en-us/library/ms131101.aspx

Andrew Babiec
initial connection vs. timing out on a subsequent operation
Does this setting affect how long the client will wait for responses from the server throughout the session, or only how long it will wait for a response to the initial connection? The text on this page indicates the latter, but perhaps this is a doc bug on Microsoft's part.

> It affects how long it will wait to create a connection. To control the timeout for queries/stored procs, look into CommandTimeout

Andrew Babiec
ConnectTimeout does not appear to work
ConnectTimeout =<n> in the connection string does not appear to work.

> You have to use "Connection Timeout=N" in the connection string.

Nick Hustak
Is there a minimum?
I've trying to ping through a list of servers to check for existence by setting the connection time out to 2 ( "Connection Timeout=2") however it doesn't seem to work. Is there an enforced minimum?

It is not a context connection
Tags :

Bart Doise
<> CommandTimeout

Set the CommandTimeout property on the Command object to specify the wait time trying to execute it. The ConnectionTimeout only configures the time to try establishing a connection to the database.


Page view tracker