SQL timeout Solution

Source: Internet
Author: User

In general, the functions that return dataset are written in this way.
Public dataset getrs (string SQL)
{
System. Data. sqlclient. sqlconnection = new sqlconnection ();
System. Data. sqlclient. sqldataadapter comm = new sqldataadapter (SQL, Conn );
System. Data. dataset DS = new dataset ();
Comm. Fill (DS );
Return Ds;
}
The new sqlconnection () contains the database connection string,
The parameter SQL is a query statement, such as SQL = "select * From s_user ".
However, if the query takes a long time (because the data in the table is large and the function is also called in the query statement), the system will return a timeout error and do not know where to control it.

In new sqlconnection (), place connect timeout = 300 in the string connecting to the database. It is incorrect because it is the time of calculation, not the time of database connection.

Therefore, we should write the statements that return dataset as follows:
Public static datatable executequery (string SQL, int cmdtimeout)
{
Sqlcommand cmd;
Sqlconnection conn;
Conn = new sqlconnection ();
Cmd = new sqlcommand (SQL, Conn );
Conn. open ();
Cmd. commandtimeout = timed timeout;
Sqldataadapter Ada = new sqldataadapter ();
Ada. selectcommand = cmd;

Dataset DS = new dataset ();
Ada. Fill (DS );
Conn. Close ();
Conn. Dispose ();
Cmd. Dispose ();
Return Ds. Tables [0];

}

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.