一般來說返回DataSet的函數都是這麼寫
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;
}
其中 new SqlConnection()裡面放串連資料庫的字串,
參數sql是查詢語句,例如sql="select * from s_user"等等
但是如果這個查詢時間比較長(因為表的裡資料比較大,而且查詢語句裡還調用了函數),系統將會返回逾時錯誤,不知道在哪裡控制
在new SqlConnection()裡面放串連資料庫的字串裡面放Connect Timeout=300;是不對的,因為這是運算的時間,不是串連資料庫的時間。
所以應該這麼寫返回DataSet的語句:
public static DataTable ExecuteQuery(string sql,int cmdTimeout)
{
SqlCommand cmd;
SqlConnection conn;
conn = new SqlConnection();
cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.CommandTimeout = cmdTimeout;
SqlDataAdapter Ada = new SqlDataAdapter();
Ada.SelectCommand = cmd;
DataSet ds = new DataSet();
Ada.Fill(ds);
conn.Close();
conn.Dispose();
cmd.Dispose();
return ds.Tables[0];
}