ASP.NET连接 MS 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;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值