我有一个运行45秒-1分钟的sp但它给我错误消息超时到期.操作完成之前经过的超时时间或服务器没有响应.我已将Connect Timeout = 120添加到连接字符串,但这没有帮助.我还在2分钟后将IIS应用
<add name="Oconnection" connectionString="Data Source=servername;Initial Catalog=dmName; User ID=username; Password=pw; Connect Timeout=120" />
这是我的cs文件:
string Oconnection=ConfigurationManager.ConnectionStrings["Oconnection"].ConnectionString;
public DataSet CreateTable()
{
DataSet dsCreateTable;
dsCreateTable = SqlHelper.ExecuteDataset(Oconnection, CommandType.StoredProcedure, "usp_CreateTables");
return dsCreateTable;
}
我是否还需要在cs文件中添加超时?
连接超时是连接到SQL Server的限制.你想要的是CommandTimeout
https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.commandtimeout?view=netframework-4.7.2
您需要在SqlCommand对象上设置CommandTimeout proptery,不能在连接字符串中设置.
您可以在代码中使用它,如下所示:
public DataSet CreateTable()
{
using(var conn = new SqlConnection(Oconnection))
{
conn.Open();
using(var command = new SqlCommand())
{
command.Connection = conn;
command.CommandTimeout = 120; // higher if needed
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "usp_CreateTables";
var da = new SqlDataAdapter();
da.SelectCommand = command;
var ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}
