Bagikan melalui


SqlConnection.ConnectionTimeout Properti

Definisi

Mendapatkan waktu untuk menunggu (dalam detik) saat mencoba membuat koneksi sebelum mengakhiri upaya dan menghasilkan kesalahan.

public:
 virtual property int ConnectionTimeout { int get(); };
public:
 property int ConnectionTimeout { int get(); };
public override int ConnectionTimeout { get; }
[System.Data.DataSysDescription("SqlConnection_ConnectionTimeout")]
public int ConnectionTimeout { get; }
member this.ConnectionTimeout : int
[<System.Data.DataSysDescription("SqlConnection_ConnectionTimeout")>]
member this.ConnectionTimeout : int
Public Overrides ReadOnly Property ConnectionTimeout As Integer
Public ReadOnly Property ConnectionTimeout As Integer

Nilai Properti

Waktu (dalam detik) untuk menunggu koneksi terbuka. Nilai defaultnya adalah 15 detik.

Penerapan

Atribut

Pengecualian

Nilai yang ditetapkan kurang dari 0.

Contoh

Contoh berikut membuat SqlConnection dan mengatur Connection Timeout ke 30 detik dalam string koneksi. Kode membuka koneksi dan menampilkan properti di jendela konsol ConnectionTimeout .

private static void OpenSqlConnection()
{
    string connectionString = GetConnectionString();
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("State: {0}", connection.State);
        Console.WriteLine("ConnectionTimeout: {0}",
            connection.ConnectionTimeout);
    }
}

static private string GetConnectionString()
{
    // 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);Initial Catalog=AdventureWorks;"
        + "Integrated Security=SSPI;Connection Timeout=30";
}
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

Keterangan

Anda dapat mengatur jumlah waktu koneksi menunggu waktu habis dengan menggunakan Connect Timeout kata kunci atau Connection Timeout di string koneksi. Nilai 0 menunjukkan tidak ada batas, dan harus dihindari dalam karena ConnectionString upaya untuk terhubung menunggu tanpa batas waktu.

Berlaku untuk

Lihat juga