如何解决c#中的连接超时错误?

时间:2016-09-21 09:24:45

标签: c# sql-server stored-procedures

我是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;";

我该如何解决?感谢。

2 个答案:

答案 0 :(得分:0)

使用CommandTimeout或优化StoredProcedure

_header: 'GET /webapp/wcs/stores/servlet/CatalogNavigationSearchResultCmd?langId=-1&storeId=12556&catalogId=33057&beginIndex=1&viewAllFlag=false&pageSize=20&searchTermScope=3&searchTermOperator=LIKE&searchType=ALL&sort_field=Relevance&searchTerm=TS19M11KRED&x=25&y=11&geoip=search HTTP/1.1\r\nreferer: http://www.topshop.com/en/tsuk/product/bags-accessories-1702216/scarves-465/feather-wings-5884878?bi=0&ps=20&geoip=prod\r\nhost: www.topshop.com\r\nConnection: close\r\n\r\n',
_headers:
  { referer: 'http://www.topshop.com/en/tsuk/product/bags-accessories-1702216/scarves-465/feather-wings-5884878?bi=0&ps=20&geoip=prod',
    host: 'www.topshop.com' },

答案 1 :(得分:0)

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

Microsoft使可选项具有良好定义的值。

超时问题有两个你需要检查的项目。

数据库: 例如MSSQL

--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

扫描完代码片段后,您会发现有两个原因导致异常。

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

  2. 你的内存在机器上安装sql server无法用来运行大查询