SqlConnection..::.ConnectionTimeout Property
This page is specific to:.NET Framework Version:
.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

'Usage

Dim instance As SqlConnection
Dim value As Integer

value = instance.ConnectionTimeout


'Declaration

Public Overrides ReadOnly Property ConnectionTimeout As Integer

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.

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


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

Community Content

SqlCommand.CommandTimeout in Regular or Context Connections
Added by:jZe
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
initial connection vs. timing out on a subsequent operation
Added by:Andrew Babiec
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
Added by:Andrew Babiec
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?
Added by:Nick Hustak
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
<> CommandTimeout
Added by:Bart Doise

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.

© 2010 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View