【问题标题】:Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated [duplicate]超时已过。在操作完成之前超时时间已过或服务器没有响应。声明已终止[重复]
【发布时间】:2013-03-06 07:56:39
【问题描述】:
using (SqlConnection connection1 = new SqlConnection(StringCon))
{
    string sSQL = string.Format("UPDATE Guest SET FirstName=@Content WHERE GuestID=@GuestID");
    SqlCommand MyCmd = new SqlCommand(sSQL, connection1);
    SqlParameter param = MyCmd.Parameters.Add("@Content", SqlDbType.NVarChar);
    param.Value = "Tony";
    SqlParameter param1 = MyCmd.Parameters.Add("@GuestID", SqlDbType.NVarChar);
    param1.Value = GuestID;
    connection1.Open();
    iResult = MyCmd.ExecuteNonQuery();
    connection1.Close();
}

但是由于这个错误,这段代码无法执行

超时。在操作完成之前超时时间已过或服务器没有响应。 声明已终止。

为什么会出现错误以及如何修复它。

【问题讨论】:

  • 手动运行该语句会发生什么?可能是表上的触发器耗时太长,或者表上没有索引,或者表被锁定等。

标签: c# winforms


【解决方案1】:

这个命令似乎比您预期的要长,您可以尝试为您的 SqlCommand 指定超时(将其扩展到您认为适合该情况的任何内容):

SqlCommand MyCmd = new SqlCommand(sSQL, connection1);
MyCmd.CommandTimeout = 1000; // in seconds

【讨论】:

  • 我试了一下,但不能执行:(,别的回答
  • 如果您直接在数据库服务器上运行相同的命令,需要多长时间?也许你需要索引你的表。您可以尝试增加 CommandTimeout。
  • 我尝试在 SQL Server 2008 中执行查询,它处理速度非常快,但是我使用代码执行非常困难,我不知道超时。
  • 你能告诉我你的连接字符串吗?
  • 数据源=(本地);初始目录=数据库名称;用户 ID=ID 名称;密码=密码;
猜你喜欢
  • 2015-12-21
  • 2012-09-23
  • 2012-01-26
  • 2011-10-08
  • 2017-05-09
  • 2012-07-24
相关资源
最近更新 更多