c# – 运行SP超过30秒时超时
发布时间:2020-12-16 01:28:21 所属栏目:百科 来源:网络整理
导读:我有一个运行45秒-1分钟的sp但它给我错误消息超时到期.操作完成之前经过的超时时间或服务器没有响应.我已将Connect Timeout = 120添加到连接字符串,但这没有帮助.我还在2分钟后将IIS应用程序池更改为超时. add name="Oconnection" connectionString="Data So
我有一个运行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; } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |