/// <summary>
        /// 返回查询结果的第一行第一列
        /// </summary>
        /// <param name="commandText">Sql语句火存储过程名称</param>
        /// <param name="prms">参数集合</param>
        /// <returns>第一行第一列</returns>
        public Object ExecuteScalar(string commandText)
        {            _cmd.CommandType = CommandType.StoredProcedure;
            _cmd.CommandText = commandText;
            try
            {
                _cmd.Connection.Open();
                return _cmd.ExecuteScalar();
            }
            catch (SqlException)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (_cmd.Connection != null && _cmd.Connection.State != ConnectionState.Closed)
                {
                    _cmd.Connection.Close();
                }
            }
        }
        /// <summary>
        /// 返回查询结果的第一行第一列
        /// </summary>
        /// <param name="commandType">Sql语句类型</param>
        /// <param name="commandText">Sql语句火存储过程名称</param>
        /// <param name="prms">参数集合</param>
        /// <returns>第一行第一列</returns>
        public Object ExecuteScalar(CommandType commandType, string commandText)
        {            _cmd.CommandType = commandType;
            _cmd.CommandText = commandText;
            try
            {
                _cmd.Connection.Open();
                return _cmd.ExecuteScalar();
            }
            catch (SqlException)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (_cmd.Connection != null && _cmd.Connection.State != ConnectionState.Closed)
                {
                    _cmd.Connection.Close();
                }
            }
        }        /// <summary>
        /// 查询集合
        /// </summary>
        /// <param name="commandText">Sql语句火存储过程名称</param>
        /// <param name="prms">参数集合</param>
        /// <returns>SqlDataReader集合</returns>
        public SqlDataReader ExecuteReader(string commandText)
        {
            SqlDataReader sr = null;
            _cmd.CommandType = CommandType.StoredProcedure;
            _cmd.CommandText = commandText;
            try
            {
                _cmd.Connection.Open();
                sr = _cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return sr;
            }
            catch (SqlException)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
        }        /// <summary>
        /// 查询集合
        /// </summary>
        /// <param name="commandText">Sql语句火存储过程名称</param>
        /// <param name="prms">参数集合</param>
        /// <returns>Datatable</returns>
        public DataTable ExecuteTable(string commandText)
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sda = null;
            _cmd.CommandType = CommandType.StoredProcedure;
            _cmd.CommandText = commandText;
            try
            {
                _cmd.Connection.Open();
                sda = new SqlDataAdapter(_cmd);
                sda.Fill(dt);
                return dt;
            }
            catch (SqlException)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (_cmd.Connection != null && _cmd.Connection.State != ConnectionState.Closed)
                {
                    _cmd.Connection.Close();
                }
            }
        }
        /// <summary>
        /// 查询集合
        /// </summary>
        /// <param name="commandType">Sql语句类型</param>
        /// <param name="commandText">Sql语句火存储过程名称</param>
        /// <param name="prms">参数集合</param>
        /// <returns>SqlDataReader集合</returns>
        public SqlDataReader ExecuteReader(CommandType commandType, string commandText)
        {
            SqlDataReader sr = null;
            _cmd.CommandType = commandType;
            _cmd.CommandText = commandText;
            try
            {
                _cmd.Connection.Open();
                sr = _cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return sr;
            }
            catch (SqlException)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
        }        /// <summary>
        /// 查询集合
        /// </summary>
        /// <param name="commandType">Sql语句类型</param>
        /// <param name="commandText">Sql语句火存储过程名称</param>
        /// <param name="prms">参数集合</param>
        /// <returns>Datatable</returns>
        public DataTable ExecuteTable(CommandType commandType, string commandText)
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sda = null;
            _cmd.CommandType = commandType;
            _cmd.CommandText = commandText;
            try
            {
                _cmd.Connection.Open();
                sda = new SqlDataAdapter(_cmd);
                sda.Fill(dt);
                return dt;
            }
            catch (SqlException)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (_cmd.Connection != null && _cmd.Connection.State != ConnectionState.Closed)
                {
                    _cmd.Connection.Close();
                }
            }
        }
    }

解决方案 »

  1.   


    cpu应该与dbhelp没关系把
    内存与dbhelp有关系  你不释放资源内存就漫了
      

  2.   

    CPU高要先确认是哪个程序,
    在程序里要看主要的循环部分。
    你的代码可是一点关系都没有。
      

  3.   

    这里有点小问题,
    private SqlCommand _cmd = null;
    private SqlParameter _param = null;
    你既然在类中维护这种继承IDisposable的非托管的对象,那么在你这个SQLHelper类中就应该实现IDisposable。应该实现手动Dispose()释放非托管资源,同时最好再实现~SQLHelper()析构函数来实现垃圾回收时自动释放非托管资源。具体可以参考非托管资源的释放。
      

  4.   


    CPU100%跟你这段代码没关系~~
      

  5.   

    你的意思是不是这样:想DataReader和SqlConnection 这样的对象都需要Dispose()释放资源?
      

  6.   

    不晓得楼主以何为依据判断瓶颈是在sqlhelper??
      

  7.   

    不知道楼主以何为依据判断问题就是在sqlhelper!!
      

  8.   


    有死锁的情况发生,发生在查询分页数据的时候,之前好像在网上看过微软petshop的SqlHelper的函数使用抽象类的形式,调用静态方法好像不会发生死锁。 所以怀疑自己写法有问题。
      

  9.   

    你的意思是不是这样:想DataReader和SqlConnection 这样的对象都需要Dispose()释放资源?我的意思是你的SQLHelper._cmd和SQLHelper._param属于SQLHelper的字段,而不是在SQLHelper的某个方法中定义的,如果定义在某个方法里面,就不用实现IDispose接口,即便在某个方法中定义的话,也最好使用using来写,这样当超出using语句块,会自动调用_cmd.Dispose()或_param.Dispose()的。而你SQLHelper的这种写法(SQLHelper._cmd和SQLHelper._param属于SQLHelper的字段)就应该实现IDispose接口。以前我也不知道应该这样写,但是我看了非托管资源的释放之后才知道的。不过我不确认这个是否就是引起你100%CPU的原因。
      

  10.   


    有死锁的情况发生,发生在查询分页数据的时候,之前好像在网上看过微软petshop的SqlHelper的函数使用抽象类的形式,调用静态方法好像不会发生死锁。 所以怀疑自己写法有问题。如果楼主想确认是否是由sqlhelper引起的,那很好办,你把你的SQLHelper换成静态的即可,然后测试下就知道原因了。
      

  11.   

    把底层数据访问层换成微软的petshop那种,CPU依然飙到了100%,最后查看日志发现只有一个异常——“死锁”,而死锁发生在查询分页数据的时候,现在做的操作是在分页查询语句后面加上WITH (NOLOCK),还在测试,先测几天看看。
      

  12.   


    是写SQL语句的问题 跟这东西没关系
      

  13.   


    分页这会也不死锁了,CPU依然飙到100%,无语了,看日志 有一个异常:HttpException 
        Exception message: A potentially dangerous Request.Path value was detected from the client (?).然后百度了一下,好像是有特殊字符才会出现这个异常,但是访问出错的路径,都正常访问,也没有特殊字符,我就是想知道,什么原因会导致CPU上升呢?