When we write a program, sometimes to make complex queries, the execution of SQL time is too long, causing the page to execute and prompt the execution of the script timeout, which is we encountered a timeout exception.
Time-out exceptions are in two cases: one is the connection timeout, and one is the execution timeout.
The former is set by Sqlconnection.connectiontimeout.
The latter is set by Sqlcommand.commandtimeout.
Sqlconnection.connectiontimeout
Gets the time to wait before terminating an attempt and generating an error when attempting to establish a connection.
The time, in seconds, to wait for the connection to open. The default value is 15 seconds.
Sqlcommand.commandtimeout
Gets or sets the wait time before terminating an attempt to execute a command and generating an error.
The time, in seconds, to wait for the command to execute. The default is 30 seconds.
This is the method found on the Internet: Conn.Open (); SqlCommand sqlcmd=new SqlCommand (); sqlcmd.commandtimeout=180; The timeout for sqlcmd is 3 minutes Can be set as needed, if too long, can also be set to 0, when this property is set to 0 means no time limit. This property value should be used with caution. You also need to set the HTTP request run time limit in the Web. config configuration file <system.web> </system.web> Set here for 720 seconds, the front property maxRequestLength is generally used for user upload file limit size! The default is typically 4096KB (4 MB). |
Another method I used in the project: Because the project needs to import at least 200,000 records at a time and classify filtering, calculation, warehousing operations. Server environment is not very good, mount too many sites, so the entire conversion process may be more than half an hour, at this time, in addition to the server may time out, the browser and server side of the session may time out, and the user does not have to wait for the task to complete. Of course, if you can see the task progress better. Workaround: 1, the introduction of static objects to record the current operating state, using AJAX real-time display task completion progress, due to the use of static objects, even if the user temporarily left the page to enter the progress is still; 2, the introduction of background threads, the operation of static objects by the thread, can be a good solution to the problem of timeouts. |
Original URL: http://www.cnblogs.com/qanholas/archive/2013/02/26/2933625.html
Asp. Net-a solution to execute a SQL timeout