Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
SqlConnection Class
 ConnectionTimeout Property
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.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)
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
ExceptionCondition
ArgumentException

The value set is less than 0.

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.

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";
}

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.

.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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
SqlCommand.CommandTimeout in Regular or Context Connections      jZe   |   Edit   |   Show History
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)
initial connection vs. timing out on a subsequent operation      BlakeU ... Andrew Babiec   |   Edit   |   Show History
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
ConnectTimeout does not appear to work      Don.Frazier ... Andrew Babiec   |   Edit   |   Show History
ConnectTimeout =<n> in the connection string does not appear to work.

> You have to use "Connection Timeout=N" in the connection string.
Is there a minimum?      Nick Hustak   |   Edit   |   Show History
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 What's this?: Add a tag
Flag as ContentBug
<> CommandTimeout      Bart Doise   |   Edit   |   Show History

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.

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker