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

Source: Internet
Author: User

I encountered a query exception today. I checked it online and thanked the original author and the translator.

If your database connection class is the data access application blocks "sqlhelper" or sqlclient class, you may encounter the following timeout exception when performing a very time-consuming SQL operation.

---------------------------

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

You will say that I have set connect timeout = 80000 in the connection string, and the timeout connection in the database is also set to a large value. Why does it still time out after 30 seconds ??

This is because:
There is no problem with your settings. It is because you have confused the differences between sqlcommand. commandtimeout and sqlconnection. connectiontimeout.
The timeout value in your connection string is only the value of sqlconnection. connectiontimeout, not the value of sqlcommand. commandtimeout.
Sqlhelper does not have sqlcommand. commandtimeout settings. You need to set it yourself.

The following are two comparisons:

Sqlcommand. commandtimeout
Obtain or set the wait time before terminating the Command Execution Attempt and generating an error.
The waiting time (in seconds ). The default value is 30 seconds.

Sqlconnection. connectiontimeout
Obtain the time that is waiting before the connection is terminated and an error is generated.
The wait time (in seconds) for the connection to open ). The default value is 15 seconds.

For more detailed descriptions of this problem, see:
Http://www.eggheadcafe.com/PrintSearchContent.asp? Linkid = 357

This problem can be regarded as a problem that cannot be taken into consideration during sqlhelper design.
The default value of sqlcommand. commandtimeout is 30. This value is sufficient for most programs I write. Therefore, sqlhelper has never been detected. I found this problem in sqlhelper only when I got a very long post (nearly 4000 replies) on a relatively poor local machine.

 

Set the timeout attribute of command!

/** // <Summary>
/// Execute the query statement and return the datatable
/// </Summary>
/// <Param name = "sqlstring"> query statement </param>
/// <Param name = "commtime"> set the query timeout </param>
/// <Returns> used for complex queries </returns>
Public static datatable getdatatable (string sqlstring, int commtime)
...{
String connectionstring = system. configuration. configurationmanager. etettings ["connectionstring"];
Using (system. Data. sqlclient. sqlconnection connection = new system. Data. sqlclient. sqlconnection (connectionstring ))
...{
Datatable dt = new datatable ();
Try
...{
Connection. open ();
System. Data. sqlclient. sqldataadapter da = new system. Data. sqlclient. sqldataadapter ();
System. Data. sqlclient. sqlcommand comm = new system. Data. sqlclient. sqlcommand (sqlstring, connection );
Comm. commandtimeout = commtime;
Da. selectcommand = comm;
Da. Fill (DT );
}
Catch (system. Data. sqlclient. sqlexception ex)
...{
Throw new exception (ex. Message );
}
Return DT;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.