SqlCommand.CommandTimeout Propriedade
Definição
Obtém ou define o tempo de espera (em segundos) antes de encerrar a tentativa de executar um comando e antes de gerar um erro.Gets or sets the wait time (in seconds) before terminating the attempt to execute a command and generating an error.
public:
virtual property int CommandTimeout { int get(); void set(int value); };
public:
property int CommandTimeout { int get(); void set(int value); };
public override int CommandTimeout { get; set; }
[System.Data.DataSysDescription("DbCommand_CommandTimeout")]
public int CommandTimeout { get; set; }
member this.CommandTimeout : int with get, set
[<System.Data.DataSysDescription("DbCommand_CommandTimeout")>]
member this.CommandTimeout : int with get, set
Public Overrides Property CommandTimeout As Integer
Public Property CommandTimeout As Integer
Valor da propriedade
O tempo de espera, em segundos, para a execução do comando.The time in seconds to wait for the command to execute. O padrão é 30 segundos.The default is 30 seconds.
Implementações
- Atributos
Comentários
Um valor de 0 indica que não há limite (uma tentativa de executar um comando aguardará indefinidamente).A value of 0 indicates no limit (an attempt to execute a command will wait indefinitely).
Observação
A CommandTimeout propriedade será ignorada por chamadas de método assíncronos APM (modelo de programação assíncrona) mais antigas, como BeginExecuteReader .The CommandTimeout property will be ignored by older APM (Asynchronous Programming Model) asynchronous method calls such as BeginExecuteReader. Ele será respeitado por métodos de toque mais recente (programação assíncrona de tarefa), como ExecuteReaderAsync .It will be honored by newer TAP (Task Asynchronous Programming) methods such as ExecuteReaderAsync.
CommandTimeout Não tem efeito quando o comando é executado em uma conexão de contexto (uma SqlConnection aberta com "context connection = true" na cadeia de conexão).CommandTimeout has no effect when the command is executed against a context connection (a SqlConnection opened with "context connection=true" in the connection string).
Observação
Essa propriedade é o tempo limite cumulativo (para todos os pacotes de rede que são lidos durante a invocação de um método) para todas as leituras de rede durante a execução do comando ou o processamento dos resultados.This property is the cumulative time-out (for all network packets that are read during the invocation of a method) for all network reads during command execution or processing of the results. Um tempo limite ainda pode ocorrer depois que a primeira linha é retornada e não inclui o tempo de processamento do usuário, somente o tempo de leitura da rede.A time-out can still occur after the first row is returned, and does not include user processing time, only network read time.
Por exemplo, com um tempo limite de 30 segundos, se o Read exigir dois pacotes de rede, ele terá 30 segundos para ler ambos os pacotes de rede.For example, with a 30 second time out, if Read requires two network packets, then it has 30 seconds to read both network packets. Se você chamar Read novamente, ele terá mais 30 segundos para ler todos os dados necessários.If you call Read again, it will have another 30 seconds to read any data that it requires.
using System;
using System.Data.SqlClient;
///
public class A {
///
public static void Main() {
string connectionString = "";
// Wait for 5 second delay in the command
string queryString = "waitfor delay '00:00:05'";
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand command = new SqlCommand(queryString, connection);
// Setting command timeout to 1 second
command.CommandTimeout = 1;
try {
command.ExecuteNonQuery();
}
catch (SqlException e) {
Console.WriteLine("Got expected SqlException due to command timeout ");
Console.WriteLine(e);
}
}
}
}