por Cesar Cassiano Schimanco

Aumentar tempo limite do SqlCommand em ASP.NET C# evitando o erro: Timeout expired

Quando precisar fazer uma consulta SQL que demore muito, devido ao grande numero de dados que ela retorna, pode ter se deparado alguma vez com o erro logo abaixo.
Por padrão o CommandTimeout do SqlCommand é de 30 segundos. No meu caso precisei do dobro para concluir sem erro.

Como aumentar tempo limite de conexão:

SqlCommand cmd = new SqlCommand(sSQL, sConn);
cmd.CommandTimeout = 120; //default 30 segundos

Erro descrito:

Server Error in '/' Application.

Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

Source Error:

Line 46: 
Line 47:                 Conn.Open();
Line 48:                 SqlDataReader dr = cmd.ExecuteReader();
Line 49:                 while (dr.Read())

 

 

Comentários

Carregando comentários

Postar um novo comentário



Processando...