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];
}