【问题标题】:How to prevent timeout error when executing a SQL Server stored procedure in ASP.NET MVC?在 ASP.NET MVC 中执行 SQL Server 存储过程时如何防止超时错误?
【发布时间】:2019-05-27 23:20:15
【问题描述】:

我有一个 C# ASP.NET MVC 程序,它在加载页面时运行存储过程。如果我从 Microsoft SQL Server Management Studio 运行存储过程,则需要 1 分钟。但是,如果我尝试从代码中运行相同的存储过程,则会超时。 我在 web.config 中添加了 Connection Timeout=0,但有时可以,有时不行。

【问题讨论】:

  • 尝试设置连接超时=0;在你的 web.config 文件的 connectionString 中
  • 我在 web.config 中添加了 Connection Timeout=0,但有时可以,有时不行。

标签: sql-server asp.net-mvc-4 stored-procedures


【解决方案1】:

你可以在调用存储过程时将超时命令设置为0。

using (SqlConnection connection = new SqlConnection(connectionString)) 
{
   connection.Open();  
   SqlCommand cmd= new SqlCommand(queryString, connection);  
   // Setting command timeout to 0 second  
   cmd.CommandTimeout = 0;  
   try 
   {  
     cmd.ExecuteNonQuery(); 
   }  
   catch(Exception ex)
   {
      // log ex here
   }
}

【讨论】:

    猜你喜欢
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多