Timeout expired , The timeout period elapsed prior to completion of the operation or the server

EnterpriseLibrary SQL超时(Timeout expired)解决方法

 

 

今天在项目中遇到一个Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding 的SQL执行超时异常,在网上google了一下,大家都遇到过我种情况,我还是第一次遇到,影响服务器产生超时的设置大致有:
1. Server.scrīptTimeout,
2. Connection对象的CommandTimeOut属性,
3. Command对象的CommandTimeOut属性,
4. IE浏览器的设置.
Server.scrīptTimeout,默认值是90秒.
要增大它,在你的asp文件中加一句,如下:
Server.scrīptTimeout=999,
将页面超时设为999秒.

最初我只设置Server.scrīptTimeout,
但仍会出现timeout错误,无论它的值设成都多大.
后在社区里看到一帖子,提到commandTimeout属性,
于是查看Option Pack文档,果然还有其他的timeout.
Connection对象和Command对象都有个CommandTimeOut属性,
连接字符串中设置了 Connect Timeout只对SqlConnection起作用。
SqlCommand.CommandTimeout
获取或设置在终止执行命令的尝试并生成错误之前的等待时间。
等待命令执行的时间(以秒为单位)。默认为 30 秒。
SqlConnection.ConnectionTimeout
获取在尝试建立连接时终止尝试并生成错误之前所等待的时间。
等待连接打开的时间(以秒为单位)。默认值为 15 秒。

SqlHelper这一点不是很让人满意啊,

最后我加了一个 SqlCommand.CommandTimeout属性,结果运行正常,问题解决.
/// <summary>执行命令超时时间</summary>
private const int TIMEOUT = 999;

public static DataSet GetDataSetByStoredProc(string sqlStoredProcName, List<SqlParameter> parameters)
        {
            DataSet ds = new DataSet();
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = null;
                dbCommand = db.GetStoredProcCommand(sqlStoredProcName);
                dbCommand.CommandTimeout = TIMEOUT;
                if (parameters != null)
                {
                    Addparameter(db, dbCommand, parameters);
                }
                ds = db.ExecuteDataSet(dbCommand);
                if (parameters != null)
                {
                    FillOutParameter(db, dbCommand, parameters);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return ds;
        }

 

 

在使用SqlHelper时出现此问题,解决方法是对设置SqlCommand的Timeout,注意不是SqlConnection的Timeout。

相关代码如下

[c-sharp] view plain copy print ?
  1. private static void PrepareCommand(SqlCommand cmd, SqlConnection conn,  
  2. SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[]  
  3. cmdParms, out bool mustCloseConnection)  
  4.     if (conn.State != ConnectionState.Open) 
  5.     { 
  6.         mustCloseConnection = true
  7.         conn.Open(); 
  8.     } 
  9.     else 
  10.     { 
  11.         mustCloseConnection = false
  12.     } 
  13.  
  14.     cmd.Connection = conn; 
  15.     cmd.CommandText = cmdText;  
  16.  
  17.     if (trans != null
  18.         cmd.Transaction = trans; 
  19.  
  20.     cmd.CommandType = cmdType;  
  21.     cmd.CommandTimeout = 240; 
  22.  
  23.     if (cmdParms != null)  
  24.     { 
  25.         foreach (SqlParameter parm in cmdParms) 
  26.             cmd.Parameters.Add(parm); 
  27.     } 
  28.     return

相关链接:

http://bytes.com/topic/asp-net/answers/331693-sqlclient-sqlexception-timeout-expired

http://blog.csdn.net/long2006sky/archive/2007/07/09/1683459.aspx

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值