共用方式為


SqlConnectionStringBuilder.ConnectTimeout 屬性

定義

取得或設定在終止嘗試並產生錯誤之前,要等待伺服器連接的時間長度 (以秒為單位)。

public:
 property int ConnectTimeout { int get(); void set(int value); };
public int ConnectTimeout { get; set; }
member this.ConnectTimeout : int with get, set
Public Property ConnectTimeout As Integer

屬性值

ConnectTimeout 屬性的值,如果未提供任何值,則為 15 秒。

範例

下列範例會先顯示未指定 "Connect Timeout" 值的連接字串內容、設定 ConnectTimeout 屬性,然後顯示新的連接字串。

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);
        }
    }
}
Imports System.Data.SqlClient
    
Module Module1

    Sub Main()
        Try
            Dim connectString As String = _
             "Server=(local);Initial Catalog=AdventureWorks;" & _
             "Integrated Security=True"
            Dim builder As 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 ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub

End Module

備註

此屬性會對應至連接字串內的 "Connect Timeout"、"connection timeout" 和 "timeout" 索引鍵。

開啟 Azure SQL 資料庫的連線時,請將連線逾時設定為 30 秒。

適用於

另請參閱