SqlConnectionStringBuilder.ConnectTimeout Property

Definition

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.

C#
public int ConnectTimeout { get; set; }

Property Value

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

Examples

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.

C#
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);
        }
    }
}

Remarks

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.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

See also