SoFunction
Updated on 2025-03-08

Solution to the timeout problem of C# million data query

This article explains in detail the solution to the timeout problem of C# million data query, and shares it for your reference. The specific methods are as follows:

Many times when we use C# to filter some information from millions of data, we often experience program connection timeout errors, and there are many common errors, such as:
Timeout expired. The timeout period elapsed prior to completion of the operation or the serveretc.

This article explains several common solutions, and those who are interested can improve and perfect it.

①. Of course, the first step is to check whether the Connection is not closed. Generally, novices make this mistake and need to check it carefully. I won’t go into details.

②. If the SQL statement is copied to the query analyzer for execution, if the execution time is already more than 30 seconds, the following solution is generally adopted:

First, analyze the reason for Timeout. Generally, Connection is not closed or timed out. Another method is caused. This method of SqlCommand is to obtain or set the waiting time before terminating the attempt to execute the command and generate an error.

hisThe default is 30 seconds. You can set it to 0. It means unlimited, but it is best not to set 0, otherwise you will wait infinitely. You only need to set this time for the query analyzer time.

③. The execution time is not very long, but the operation timeouts still. There are many reasons. There are two common reasons: the application's request timeout, or the connection lifetime of the connection pool has passed, because the default value of the connection pool is 60 seconds, so for these two solutions:

Resolve application request timeout:

Add the following statement to:

<> 
<httpRuntime maxRequestLength="102400" executionTimeout="720" />
</> 

executionTimeout: is the maximum time limit for allowing execution of requests, in seconds
maxRequestLength: Indicates the maximum file upload size supported. This limitation can be prevented. Everyone understands it.

Solve the problem of program pool survival cycle:

Modify in the database connection string:

database=AA;
uid=sa;
pwd=sa; 
Pooling=true;
MAX Pool Size=1024;
Min Pool Size=1;
Connection Lifetime=60

As for the meaning, you basically understand from the English meaning, haha. Of course, there are many other requirements for the Min Pool Size=1 setting.

I hope this article will be helpful to everyone's C# programming.