郁闷,连接池问题越来越严重了

using System;
using System.Data.SqlClient;
using System.Data;
namespace ASPNetPortal
{
 /// <summary>
 /// It is used to access database
 /// </summary>
 /// -----------------------------------------------------
 /// -----------------------------b0b0 2004-9.3----------
 public class Database:IDisposable
 {   
  /// <summary>
  /// It's store the error message
  /// </summary>
  string _errorMessage;
  /// <summary>
  ///It's store the Exception
  /// </summary>
  System.Exception _exception;
  /// <summary>
  /// It is marked run state.
  /// </summary>
  int result;
  /// <summary>
  /// store Connection String
  /// </summary>
  //string _connectionString;

  SqlConnection myConnection=null;

  SqlCommand myCommand;

  string _ConfigurationConnectionString=null;
    #region IDisposable 成员

  public void Dispose()
  {
   if(this.myConnection==null)
    myConnection.Dispose();
   else
    myConnection=null;
   System.GC.SuppressFinalize(this);
  }

  #endregion
/// <summary>
/// get or set  Exception as string .
/// </summary>
///
  public string ErrorMessage
  {
   get{ return _errorMessage;}
   set{ _errorMessage=value;}


  }
  public int Result
  {
   get{return result;}
  }

  /// <summary>
  /// get or set Exception.
  /// </summary>
  public Exception Exception
  {
   get{return _exception;}
   set{_exception=value;}

  }
  /// <summary>
  /// Set ConnectionString where in Web.Config
  ///( rewrite a bug that can't Set ConfigurationConnectionString 2004-10-8 b0b0)
  ///
  /// </summary>
  public string ConfigurationConnectionString
  {
   get{return this._ConfigurationConnectionString;}
   set{this._ConfigurationConnectionString=value;}
  }

  /// <summary>
  /// get Connetion string from web.Config.if you hava not set the ConfigurationConnection.
  /// it will get AppSettings by connectionString,if you want set the ConnectionString you
  /// can use it.
  /// rewrite a bug(property ConnectionString when this._ConfigurationConnectionString!=null
  /// return a error ConnectionString) 2004-10-8
  /// </summary>
   private string ConnectionString
  {
   get{
    if(this._ConfigurationConnectionString==null)
    {
     return (string)System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
    }
    else
    {
                   return (string)this._ConfigurationConnectionString;

    }
   }
         }
  
  //----------------------------------------------------------------
  //Modify by  b0b0 2005-04-18
  //Description:Modify turn off  Connection
  public void CloseConnection()
  {
   if(myConnection.State==System.Data.ConnectionState.Open)
   {
    myConnection.Close();
   }
  }

  public Database()
  {
   myConnection=new SqlConnection(this.ConnectionString);
  }
  //----------------------------------------------------------------
  /// <summary>
  /// Create OutPut SqlParameter.
  /// </summary>
  /// <param name="_name">Parameter's Name</param>
  /// <param name="_value">Parameter's Value</param>
  /// <returns></returns>
  public SqlParameter MakeParameter(string _name,object _value)
  {
   
    SqlParameter myParameter=new SqlParameter(_name,_value);
    myParameter.Direction=ParameterDirection.Input;
    return myParameter;
  }
  public SqlParameter MakeParameter(string _name,object _value,ParameterDirection  _direction)
  {
   
   SqlParameter myParameter=new SqlParameter(_name,_value);
   myParameter.Direction=_direction;
   return myParameter;
  }


  /// <summary>
  ///
  /// </summary>
  /// <param name="_name"></param>
  /// <param name="_value"></param>
  /// <param name="_direction"></param>
  /// <returns></returns>
  public SqlParameter MakeParameter(string _name,System.Data.SqlDbType _DbType,ParameterDirection    _direction)
  {
    SqlParameter myParameter=new SqlParameter(_name,_DbType);
          myParameter.Direction=_direction;
     return myParameter;
    
   }
  public SqlParameter MakeParameter(string _name,System.Data.SqlDbType _DbType,ParameterDirection  _direction,int _size)
  {
   SqlParameter myParameter=new SqlParameter(_name,_DbType);
   myParameter.Direction=_direction;
   myParameter.Size=_size;
   return myParameter;
    
  }
  /// <summary>
  /// The Procedure which have no parameter and
  /// return scalar.
  /// </summary>
  /// <param name="_procedureName"></param>
  /// <returns></returns>
  public void RunProcedure(string _procedureName)
  {
   try
   {
    //myConnection=new SqlConnection(this.ConnectionString);
    myCommand=new SqlCommand(_procedureName,myConnection);
    myCommand.CommandType=CommandType.StoredProcedure;
    myConnection.Open();
    //this.result=myCommand.ExecuteNonQuery();
    //another bug when myCommand.Executor CommandText which contain UPDATE、INSERT 和 DELETE
    //myCommand.ExecuteNonQuery()==-1 and u set this.result=-1,at last  u  make user felt there must have Exception ,but there has not .
    //----Rewrite at 2004-10-20 b0b0----------------------------------------------------------
    myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
  
   }
   catch(Exception myEx)
   {
    this.ErrorMessage=myEx.Message;
    this.Exception=myEx;
    this.result =-1;
   }
   finally
   {
    myConnection.Close();


   }
           }

        /// <summary>
        /// The Procedure which have none parameter and return SqlDataReader.
        /// </summary>
        /// <param name="_procedureName"></param>
        /// <returns></returns>
  public void RunProcedure(string _procedureName, ref SqlDataReader _datareader)
  {
   try
   {
    //myConnection=new SqlConnection(this.ConnectionString);
    myCommand=new SqlCommand(_procedureName,myConnection);
    myCommand.CommandType=CommandType.StoredProcedure;
    myConnection.Open();
    _datareader=myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
   }
   catch(Exception myEx)
   {
    this.ErrorMessage=myEx.Message;
    this.Exception=myEx;
    this.result=-1;
    
   }
  }
  /// <summary>
  /// The Procedure which have none parameter and return DataSet.
  /// </summary>
  /// <param name="_procedureName"></param>
  
 public void RunProcedure(string _procedureName,ref DataSet _dataset)
  {   
   try{
   //myConnection=new SqlConnection(this.ConnectionString);
   myCommand=new SqlCommand(_procedureName,myConnection);
   myCommand.CommandType=CommandType.StoredProcedure;
   myConnection.Open();
   SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
   myAdapter.Fill(_dataset);
   }
   catch(Exception myEx)
   {
    this.ErrorMessage=myEx.Message;
    this.Exception=myEx; 
    result=-1;
    
   }
   finally
   {
    myConnection.Close();

   }

}
  /// <summary>
  /// The Procedure which has Parameter's Array and return SqlDataReader.
  /// </summary>
  /// <param name="_procedureName"></param>
  /// <param name="_parameters"></param>
  /// <returns></returns>
  public void RunProcedure(string _procedureName,SqlParameter[] _parameters,ref SqlDataReader _sqldataReader)
  {
   try
   {
    //myConnection=new SqlConnection(this.ConnectionString);
    myCommand=new SqlCommand(_procedureName,myConnection);
    myCommand.CommandType=CommandType.StoredProcedure;
    foreach(SqlParameter param in _parameters)
     myCommand.Parameters.Add(param);

    myConnection.Open();
    _sqldataReader=myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
   }
   catch(Exception myEx)
   {
    this.ErrorMessage=myEx.Message;
    this.Exception=myEx;
    result=-1;

    
   }
  }
  /// <summary>
  /// The Procedure which has Parameter's Array and return DataSet.
  /// </summary>
  /// <param name="_procedureName"></param>
  /// <param name="_parameters"></param>
  /// <returns></returns>
  public void RunProcedure(string _procedureName,SqlParameter[] _parameters,ref DataSet _dataset)
  {
   try
   {
    //myConnection=new SqlConnection(this.ConnectionString);
    myCommand=new SqlCommand(_procedureName,myConnection);
    myCommand.CommandType=CommandType.StoredProcedure;
    foreach(SqlParameter param in _parameters)
     myCommand.Parameters.Add(param);
                myConnection.Open();
    SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
    myAdapter.Fill(_dataset);
   }
   catch(Exception myEx)
   {
    this.ErrorMessage=myEx.Message;
    this.Exception=myEx;
    result=-1;
    
   }
   finally
   {
    myConnection.Close();


   }
  }
  /// <summary>
  /// The Procedure which has Parameter's Array and return Scale.
  /// </summary>
  /// <param name="_procedureName"></param>
  /// <param name="_parameters"></param>
  /// <returns></returns>
  public void RunProcedure(string _procedureName,SqlParameter[] _parameters)
  {
   try
   {
    //myConnection=new SqlConnection(this.ConnectionString);
    myCommand=new SqlCommand(_procedureName,myConnection);
    myCommand.CommandType=CommandType.StoredProcedure;
    foreach(SqlParameter param in _parameters)
     myCommand.Parameters.Add(param);

    myConnection.Open();
    myCommand.ExecuteNonQuery();
   }
   catch(Exception myEx)
   {
    this.ErrorMessage=myEx.Message;
    this.Exception=myEx;
    result=-1;
    
   }
   finally
   {
    myConnection.Close();

   }
  }

    }
}

到底问题出在那块儿?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值