system.data.sqlclient.sqlexception:timeout expired

今天在测试一个通过wcf服务查询数据的程序时,一直出现这个错误。这个错误是在:

catch (System.ServiceModel.CommunicationException e)             

 {

      XtraMessageBox.Show("对不起!客户端获取服务数据通信报错!请查看服务是否正常!"+e.HelpLink+","+e.InnerException+","+e.Message+","+e.StackTrace+","+e.TargetSite+"", "系统提示", MessageBoxButtons.OK,                                                    MessageBoxIcon.Information);       }             

 开始我一直在客户端找原因,以为是调用服务的时候超时,经过一天半的纠结,崩溃,最后找出了,是由于执行sql语句或者存储过程时,因为涉及运算量巨大,所有超时。这个超时被服务端捕获到,然后传递到了客户端,所有在客户端一直会弹出这个错误。

解决办法:

using (SqlConnection conn = new SqlConnection(connectionString))       

 {          conn.Open();     

            DataTable dt = new DataTable();            

           SqlCommand cmd = new SqlCommand();      

           cmd.CommandType = CommandType.StoredProcedure;    

           cmd.CommandText = cmdText;       

           cmd.Connection = conn;    

           cmd.CommandTimeout = 300;//给执行加了个时间    

           if (paras != null)            

       {                   

        foreach (SqlParameter p in paras)             

                cmd.Parameters.Add(p);            

       }               

      try      

           {                  

          SqlDataAdapter da = new SqlDataAdapter(cmd);        

               da.Fill(dt);       

          }              

     catch (Exception ex)                 {  throw ex;  }      

         finally                

     {  cmd.Dispose();}            

      return dt;        

     }

查询数据量一定时,除了算法优化,查询速度还跟电脑的cpu等配置有关。

You can reach me by surfing the web ---- huntjobs.cn,or sending e-mails to me,Here is my qq MailBox:1424870395@qq.com
原文地址:https://www.cnblogs.com/HedgehogBlog/p/3490668.html