通过ASP.NET codebehind执行超时,在SQL Server Management Studio中不会发生此情况
在SQLServerManagementStudio中直接运行我的查询时,它工作正常,大约需要4秒钟 当我在ASP.NET应用程序中运行它时,我得到: 执行超时已过期。操作完成前已过超时时间,或者服务器没有响应 即使我确保已将超时添加到connectionstring,请参见我的代码:通过ASP.NET codebehind执行超时,在SQL Server Management Studio中不会发生此情况,asp.net,sql-server,timeout,Asp.net,Sql Server,Timeout,在SQLServerManagementStudio中直接运行我的查询时,它工作正常,大约需要4秒钟 当我在ASP.NET应用程序中运行它时,我得到: 执行超时已过期。操作完成前已过超时时间,或者服务器没有响应 即使我确保已将超时添加到connectionstring,请参见我的代码: web.config: <connectionStrings> <add name="conn1" connectionString="data source=(lo
web.config
:
<connectionStrings>
<add name="conn1"
connectionString="data source=(local)\sqlexpress;Initial Catalog=mydb;User Id=myuser;Password=mypwd;"
providerName="System.Data.SqlClient" />
</connectionStrings>
为什么这种行为会有所不同?您希望使用而不是
这是典型的参数嗅探坏了的声音。解决这个问题的正确方法不是增加超时。您需要解决性能问题。@SeanLange:谢谢。但是,如果性能很差,为什么它在MSSQL Management Studio中运行得很好?你的链接谈到了StoredProc,而我没有使用它们。我猜是因为你实际上没有共享任何sql。我不可能知道您是在使用存储过程还是信鸽。您正在使用参数化查询吗?你能分享你的疑问以便我们能提供帮助吗?@SeanLange-LOL!好的,说得好:)我尝试了命令timeout而不是connectiontimeout,现在它可以工作了。
Public Shared Function GetConnection() As SqlConnection
Dim MyConnection As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conn1").ToString)
Return MyConnection
End Function
Dim myConnection As SqlConnection = GetConnection()
myConnection.ConnectionString = ConfigurationManager.ConnectionStrings("conn1").ToString + "Connection Timeout=900" 'timeout in seconds
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim command As New SqlCommand(queryString, connection)
command.CommandTimeout = 900
...
End Using