不能将CommandTimeout设置为大于90秒
本文关键字:大于 90秒 设置 CommandTimeout 不能 | 更新日期: 2023-09-27 18:01:18
我在Visual Studio 2008中使用c#代码编写了一个asp.net web应用程序。
我有一个SQL查询查询另一台服务器上的SQL Server数据库。当我运行查询时,它在90秒后超时。我已经尝试了各种不同的设置。
我在网上搜遍了,但仍然找不到答案。我在代码中有一行为查询设置CommandTimeout
。如果我将其设置为CommandTimeout = 1;
,查询将在1秒后超时,如果我设置为CommandTimeout = 90;
,查询将在90秒后超时。
这一切都很好,但我的查询需要大约。还有150秒。如果我将代码更改为CommandTimeout = 200;
,查询仍然会在90秒后超时。似乎我只能更改超时时,它少于90秒。任何超过90秒的内容仍然会在90秒内超时。
这快把我逼疯了。是否有其他地方的设置重写了我的代码?
我的代码
// bind the data to the Gridview
private void BindTaskList()
{
string startDate = StartDate.Text;
string endDate = EndDate.Text;
// Create a connection string referring to the connection string from web.config file
string conStr = ConfigurationManager.ConnectionStrings["Docupro_ReportingConnectionString"].ConnectionString;
SqlConnection sqlConnection = new SqlConnection(conStr);
// This is the SQL query and must be in one long line
SqlCommand sqlCommand = new SqlCommand("SELECT T5.DisplayName AS 'User', T2.LongName AS 'Print Type', SUM(T1.Quantity) AS 'Total Quantity', '£'+CONVERT(varchar, SUM(T1.Amount), 3) AS 'Total Cost' FROM tblTransaction T1 JOIN tblItem T2 ON T1.ItemID = T2.ItemID JOIN tblLedger T3 ON T1.LedgerID = T3.LedgerID JOIN tblTender T4 ON T1.TenderID = T4.TenderID JOIN tblCustomer T5 ON T4.CustomerID = T5.CustomerID JOIN tblTerminal T6 on T1.TerminalID = T6.TerminalID JOIN tblStation t7 on T6.StationID = t7.StationID WHERE (TransactionDateTime BETWEEN @StartDate AND @EndDate)AND T3.LongName = 'Not Assigned' GROUP BY T5.DisplayName, T2.LongName ORDER BY T5.DisplayName", sqlConnection);
// Create the parameters from the text boxes and drop down list
sqlCommand.Parameters.Add(new SqlParameter("@StartDate", startDate));
sqlCommand.Parameters.Add(new SqlParameter("@EndDate", endDate));
// Set the command timeout to 200 seconds to allow for long queries
sqlCommand.CommandTimeout = 200;
sqlConnection.Open();
// Create a DataSet to fill with data
SqlDataAdapter myAdapter = new SqlDataAdapter(sqlCommand);
DataSet myDataSet = new DataSet();
myAdapter.Fill(myDataSet);
// Turn off GridView Footer
GridView1.ShowFooter = false;
// Fill the GridView with the DataSet
GridView1.DataSource = myDataSet;
GridView1.DataBind();
}
万分感谢
安迪错误信息是:
Sys.WebForms。PageRequestManagerTimeoutException:服务器请求超时ScriptResource.axd
代码:0
错误消息显示超时来自ASP。.NET(不是ADO.NET)。设置Server.ScriptTimeout=200
.
解释错误消息是调试错误的第一步。当你读到"超时"的时候不要停下来。阅读并解释所有内容
FWITW…
我有一个错误(与ASP无关-这是在控制台应用程序中),我发现在设置任何参数后设置CommandTimeout 似乎没有生效(即使属性已更新)
如
SqlCommand comm = new SqlCommand(conn, proc);
comm.Parameters.AddWithValue("@a",123);
comm.CommandTimeout = 300;
comm.ExecuteReader(); //Times out at 30 seconds
,
SqlCommand comm = new SqlCommand(conn, proc);
comm.CommandTimeout = 300;
comm.Parameters.AddWithValue("@a",123);
comm.ExecuteReader(); //Times out at 300 seconds
我确实有一个返回SqlCommand的函数,然后在返回时执行它,但我猜测这个简化版本会有相同的结果。
总结尝试在创建SqlCommand后立即设置CommandTimeout属性
您试过将其设置为0
吗?
评论
0表示没有限制,在CommandTimeout中应该避免使用,因为尝试执行命令将无限期地等待。
exists TimeOut in SQLcommand并在SqlConnection
中存在TimeOut ServerPath = "Data Source=" + myDr["server1"].ToString();
ServerPath += ";Initial Catalog=" + myDr["catalog1"].ToString();
ServerPath += ";uid=" + myDr["username"].ToString();
ServerPath += ";pwd=" + myDr["password1"].ToString();
ServerPath += ";Connect Timeout=" + TimeOuted.ToString();
conn1.ConnectionString = ServerPath;