4

我在 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

4

4 回答 4

6

错误消息显示超时来自 ASP.NET(而不是来自 ADO.NET)。设置Server.ScriptTimeout=200

解释错误消息是调试任何错误的第一步。不要只是在阅读“超时”时停下来。阅读和解释一切。

于 2013-02-27T13:54:14.253 回答
2

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 属性。

于 2016-02-08T12:04:22.770 回答
0

您是否尝试将其设置为0?

评论

值 0 表示没有限制,应在 CommandTimeout 中避免,因为尝试执行命令将无限期地等待。

这是参考。

于 2013-02-27T12:19:17.593 回答
0

SQLcommand中存在TimeOut,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;
于 2017-10-24T10:42:14.443 回答