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 秒。

适用于

另请参阅