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属性,结果运行正常,问题解决.
///

执行命令超时时间
private const int TIMEOUT = 999;

public static DataSet GetDataSetByStoredProc(string sqlStoredProcName, List 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

private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms, out bool mustCloseConnection) { if (conn.State != ConnectionState.Open) { mustCloseConnection = true; conn.Open(); } else { mustCloseConnection = false; } cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = cmdType; cmd.CommandTimeout = 240; if (cmdParms != null) { foreach (SqlParameter parm in cmdParms) cmd.Parameters.Add(parm); } return; }

相关链接:

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

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

 

 

你可能感兴趣的:(Timeout expired , The timeout period elapsed prior to completion of the operation or the server)