首页 > 开发 > SQL Server > 正文

System.Data.SqlClient.SqlException: 超时时间已到---解决之道

2019-10-19 21:47:04
字体:
来源:转载
供稿:网友
前几天在给公司做一个统计日报页面,因为要查询的数据量非常巨大,改为存储过程来执行查询依然速度缓慢,更为郁闷的是当我测试此页面的时候,总是出错——System.Data.SqlClient.SqlException: 超时时间已到。

     解决之道分为两步:

     1.在web.config中的<system.web>标签中插入下面这句:

        <httpRuntime maxRequestLength="2097151" executionTimeout="720000"/>

        maxRequestLength的最大值好像就是2097151了,再大了会报错。executionTimeout的值完全是我乱写的,但至少还没报错。

     2.在连接数据库的代码中加入如下一句:

       string ConStr = "Data Source=……;Initial Catalog=……;Persist Security Info=True;User ID=……;Password=……";
        SqlConnection conn = new SqlConnection(ConStr);
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = this.sql;
        cmd.CommandTimeout = 10000;   //要加这一句
        conn.Open();
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        conn.Close();

    

       如果这样修改之后还有问题,那你真得考虑是不是要更换你的服务器了~……呵呵!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表