超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。看SqlHelper代码
        public static DataTable ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();
            SqlConnection conn = new SqlConnection(connectionString);            // we use a try/catch here because if the method throws an exception we want to 
            // close the connection throw code, because no datareader will exist, hence the 
            // commandBehaviour.CloseConnection will not work
            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);                DataSet ds = new DataSet();
                SqlDataAdapter ada = new SqlDataAdapter(cmd);
                ada.Fill(ds);
                cmd.Parameters.Clear();
                return ds.Tables[0];
            }
            catch
            {
                conn.Close();
                throw;
            }
        }
        /// <summary>
        /// Prepare a command for execution(数据访问层方法)
        /// </summary>
        /// <param name="cmd">SqlCommand object(SQL命令)</param>
        /// <param name="conn">SqlConnection object(连接对象)</param>
        /// <param name="trans">SqlTransaction object(事务)</param>
        /// <param name="cmdType">Cmd type e.g. stored procedure or text(键入cmd存储过程或文本)</param>
        /// <param name="cmdText">Command text, e.g. Select * from Causewares(SQL语句)</param>
        /// <param name="cmdParms">SqlParameters to use in the command(SQL语句参数)</param>
        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) {
            //如果数据库没有打开的话,就打开            if (conn.State != ConnectionState.Open)
                conn.Open();            cmd.Connection = conn;//SQL命令中的链接对象就是传过来的连接对象(数据服务器,账号,密码)            cmd.CommandText = cmdText;//SQL命令中的SQL语句就是传过来的SQL语句            //如果没有事务的话            if (trans != null)
                cmd.Transaction = trans;//SQL命令的事务就等于传过来的事务
            cmd.CommandType = cmdType;//SQL命令的储存过程就等于传过来的储存过程            //如果SQL语句有参数的话
            //遍历所有参数添加到SQL语句对应的参数地址            if (cmdParms != null) {
                foreach (SqlParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }//
//这里是在用到的地方使用的
//直接 return DataTable 
            using (DataTable dt = SqlHelper.ExecuteDataSet(SqlHelper.ConnectionStringLocalTransactionSAP,
                                                            CommandType.Text,
                                                            Sql))
            {
                return dt;
            }
帮我看看是不是没有关掉连接,,,这个是我刚刚从别人手中接过来的项目,一执行导出Excel就出现了“连接越时”   数据库的活动连接立马就达到了100多个帮忙看看

解决方案 »

  1.   


      private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) {
                //如果数据库没有打开的话,就打开            if (conn.State != ConnectionState.Open)
                    conn.Open();            cmd.Connection = conn;//SQL命令中的链接对象就是传过来的连接对象(数据服务器,账号,密码)            cmd.CommandText = cmdText;//SQL命令中的SQL语句就是传过来的SQL语句            //如果没有事务的话            if (trans != null)
                    cmd.Transaction = trans;//SQL命令的事务就等于传过来的事务
                cmd.CommandType = cmdType;//SQL命令的储存过程就等于传过来的储存过程            //如果SQL语句有参数的话
                //遍历所有参数添加到SQL语句对应的参数地址            if (cmdParms != null) {
                    foreach (SqlParameter parm in cmdParms)
                        cmd.Parameters.Add(parm);
                }
            }
    这里有conn.open()的方法,没有close(),当然会这样了。一定要释放连接。
      

  2.   

    不好意思,没有看清楚,还有第一段代码catch
                {
                    conn.Close();
                    throw;
                }
    conn.Close();应该放在finally里面
    因为你只有捕获到异常之后才能关闭连接。
    事实上是每次执行完都要关闭连接finally
    {
       conn.Close();
    }
      

  3.   


    你说的是        //我想问一下这个是MS官方的吗???,还是自己写的,,,因为这个我从别人手中接过来项目
            public static DataTable ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
            {
                SqlCommand cmd = new SqlCommand();
                SqlConnection conn = new SqlConnection(connectionString);            // we use a try/catch here because if the method throws an exception we want to 
                // close the connection throw code, because no datareader will exist, hence the 
                // commandBehaviour.CloseConnection will not work
                try
                {
                    PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);                DataSet ds = new DataSet();
                    SqlDataAdapter ada = new SqlDataAdapter(cmd);
                    ada.Fill(ds);
                    cmd.Parameters.Clear();
                    return ds.Tables[0];
                }
                catch    //的是在这里出现了导常之后,才会关闭吗???
                {
                    conn.Close();
                    throw;
                }             //我应该还要加一句,,,也要关闭的
                   //还有就是,我加了这句后,对正个项目有影响没有啊,,,,有很多很多地方有用到的啊
                 finally
                 {
                      conn.Close();
                      throw;//这个还要不啊!
                 }
            }
      

  4.   

    难到是是只要关闭  conn.Close();  的问题吗?????????求解啊!!!!!!
      

  5.   

    throw是抛出异常,在finally里面不需要的,你每次调用 ExecuteDataSet都会实例化一个sqlconnection,然后你每次都不关闭它,当然会超出连接数的范围了,每次实例化后的对象都是不一样的,虽然他们的名字是一样的。所以,还是要关闭。
      

  6.   

                 finally
                 {
                      conn.Close();
                      
                 }
    加了这个根本就不行啊!!!!!!!
    怎么办啊!!!!!!!!
      

  7.   

    改成finally
      {
      conn.Close();
      conn.Dispose()
      }试试,再检查是不是其他地方有问题。