Export (0) Print
Expand All
2 out of 2 rated this helpful - Rate this topic

SqlConnectionStringBuilder.ConnectTimeout Property

Gets or sets the length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error.

Namespace:  System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)
public int ConnectTimeout { get; set; }

Property Value

Type: System.Int32
The value of the ConnectTimeout property, or 15 seconds if no value has been supplied.

This property corresponds to the "Connect Timeout", "connection timeout", and "timeout" keys within the connection string.

When opening a connection to a Azure SQL Database, set the connection timeout to 30 seconds.

The following example first displays the contents of a connection string that does not specify the "Connect Timeout" value, sets the ConnectTimeout property, and then displays the new connection string.

using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        try
        {
            string connectString =
                "Server=(local);Initial Catalog=AdventureWorks;" +
                "Integrated Security=true";
            SqlConnectionStringBuilder builder =
                new SqlConnectionStringBuilder(connectString);
            Console.WriteLine("Original: " + builder.ConnectionString);
            Console.WriteLine("ConnectTimeout={0}",
                builder.ConnectTimeout);
            builder.ConnectTimeout = 100;
            Console.WriteLine("Modified: " + builder.ConnectionString);

            Console.WriteLine("Press any key to finish.");
            Console.ReadLine();

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.