Timeout for SqlDataSource



我在VS 2010中保留了一个我继承的C#应用程序,但我遇到了超时问题。

正在调用的存储过程现在需要更长的时间来执行,正在生成以下错误:

无法检索记录数据:超时已过期。操作完成之前经过的超时期限或服务器没有响应。

使用SqlDataSource直接在代码中实例化,我不知道如何设置CommandTimeout属性,因为它似乎不可用。这是真的吗?如果不是,我如何访问该属性?

我看到的解决方案在.aspx文件中具有SqlDataSource,通常CommandTimeout是在控件(例如:gridview(事件中设置的。这里的情况并非如此。

编辑:

一些代码

Results = new SqlDataSource();
Results.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["EM"].ConnectionString;
Results.SelectCommandType = SqlDataSourceCommandType.Text;
Results.SelectCommand = "exec sproc";
DataView ReturnedDataSet = (DataView)Results.Select(new 
System.Web.UI.DataSourceSelectArguments());

谢谢。

虽然我强烈建议您将 SqlDataSource 移开(运行!(,但您可以从 C# 设置命令超时。有一个事件,您可以将事件处理程序连接到该事件,这将公开 DbCommand,您可以在其中设置超时。

var ds = new SqlDataSource("connectionString", "select * from dbo.Project_Master");
ds.Selecting += (object s, SqlDataSourceSelectingEventArgs e) => e.Command.CommandTimeout = 1000;
var dataView = (DataView)ds.Select(new DataSourceSelectArguments());

最新更新