【问题标题】:How can solve connection time out error in c#?如何解决c#中的连接超时错误?
【发布时间】:2016-09-21 09:24:45
【问题描述】:

我是 C# 和 SQL Server 的新手;我在 T-SQL 和 C# 中编写了一个简单的存储过程,代码调用如下:

da = new SqlDataAdapter("select_equals_Cycle", con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
ds = new DataSet(); 
da.Fill(ds, "select_equals_Cycle");

但在这一行:

da.Fill(ds, "select_equals_Cycle");

我收到此错误:

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

我的连接字符串是这样的:

string conn = "Data Source=.;Initial Catalog=ClubEatc;Integrated Security=True;Connect Timeout=30;";

我该如何解决?谢谢。

【问题讨论】:

  • 你需要找到超时的根本原因。是因为您试图从数据库中加载数以亿计的千兆字节吗?你在等锁吗?你的网络真的很慢吗?您网络吗?你总是可以增加超时时间,但除非你明白为什么它不会真正成为一个修复,更像是一个创可贴。
  • 好吧,如果所有其他方法都失败了,您可以增加连接字符串中的超时时间 - 如果过程运行那么长时间 - 但是如果存储过程运行超过 30 秒来填充数据集,则该过程是相当低效或服务器未设置为您需要的负载。
  • 试试这个链接stackoverflow.com/questions/8602395/…中的步骤

标签: c# sql-server stored-procedures


【解决方案1】:

使用 CommandTimeout 或优化 StoredProcedure

da.SelectCommand.CommandTimeout = 180; // default is 30 seconds

【讨论】:

    【解决方案2】:

    如果不知道如何配置,请不要设置超时值。

    微软使可选具有明确定义的价值。

    超时问题有两项您必须检查。

    数据库: 例如微软SQL

    --the code below ran in Query of Sql Server 
    --this code snippet will show you all the advance options
    Exec sp_configure 'show advanced options',1
    recogfigure
    -- this code snippet configure the seconds of the query wait 
    -- this configuration means that if the  memory couldn't used to query -- the big query, sql server will wait for some seconds
    -- this option usually use the default value made by Microsoft
    -- the default value about how many seconds of formula is the estimated query -- -- time multiply by 25, and if the memory still couldn't used for querying.
    -- then throw TimeOut Exception
    Exec sp_configure 'query wait', 200;
    ReCONFIGURE
    -- the end
    
    // the timeout configuration about C#
    SqlConnection.ConnectionTimeout = 200
    // here is the Document:
    // get the time to wait while trying to establish a connection before 
    // terminating the attempt and generating  and error 
    // the Unit of the property is second
    // 0  is no limit
    

    扫描代码sn-p后,你会发现有两个原因会导致异常。

    1. 您的代码无法建立连接。

    2. 您在机器安装 sql server 中的内存无法用于运行大查询

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-02
      • 2021-06-01
      • 1970-01-01
      相关资源
      最近更新 更多