sqlhelper有自动关闭的,但是在mysql中怎么监视比如说总共有五个连接,如果连接数大于5就是未释放的
但是这个统计连接数 我怎么看呢using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using System.Web;
using System.Configuration;namespace csBll
{
    public class MySqlConn
    {
        public MySqlConn()
        {        }
        private string database = string.Empty;
        public MySqlConn(string databaseName)
        {
            this.database = databaseName;
        }        public MySqlConnection CreateConn()
        {
            string connStr = HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings["mysqlStr"]);
            string[] connStrs = connStr.Split('\\');
            string connString = connStrs[connStrs.Length - 1].Replace("meetme",database);
            MySqlConnection myConn = new MySqlConnection(connString);
            return myConn;
        }        public bool ExecSQL(string sqlStr)
        {
            MySqlConnection myConn = CreateConn();
            myConn.Open();
            MySqlCommand cmd = myConn.CreateCommand();
            cmd.CommandText = sqlStr;
            if (cmd.ExecuteNonQuery() > 0)
            {
                myConn.Close();
                return true;
            }
            else
            {
                myConn.Close();
                return false;
            }
        }        public MySqlDataReader ReturnDataSet(string sqlStr)
        {
            MySqlConnection myConn = CreateConn();
            myConn.Open();
            MySqlCommand cmd = myConn.CreateCommand();
            cmd.CommandText = sqlStr;            MySqlDataReader rd = cmd.ExecuteReader();            //myConn.Close()-------这个注释了影响吗
            return rd;
        }        public System.Data.DataSet ExecuteDataSet(string p)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}

解决方案 »

  1.   

    释放连接。C# 调用 MYSQL 会出现连接池溢出,1.你可以用 Using(){} 对 有继承 IDisposable 接口的对象进行处理。
    2.你直接继承 IDisposable 接口 实现这个接口。
      

  2.   

    这样吧 ,我直接给你个 代码, 正好我前些日期写的, MYSQL ADO.NET 访问类,有不合理的地方 你给我留言。代码如下:using MySql.Data.MySqlClient;
        using System;
        using System.Collections.Generic;
        using System.Data;
        using System.Reflection;
        using ESWS;    internal class MySqlHelper : IDisposable
        {
            private MySqlCommand mySqlCommand;
            private MySqlConnection mySqlConnection;
            private MySqlDataAdapter mySqlDataAdapter;
            private MySqlTransaction myTransaction;        protected virtual void AddParameters(MySqlCommand command, MySqlParameter[] parameters)
            {
                foreach (MySqlParameter parameter in parameters)
                {
                    command.Parameters.AddWithValue(parameter.ParameterName, parameter.Value);
                }
            }        internal void BegionTransaction()
            {
                if (this.DBConnection.State != ConnectionState.Open)
                {
                    this.DBConnection.Open();
                }
                this.myTransaction = this.DBConnection.BeginTransaction();
            }        internal void Commit()
            {
                this.DBTransaction.Commit();
            }        protected MySqlCommand CreateCommand(string commandText)
            {
                return this.CreateCommand(commandText, CommandType.Text);
            }        protected MySqlCommand CreateCommand(string commandText, CommandType commandType)
            {
                return this.CreateCommand(commandText, commandType, null);
            }        protected MySqlCommand CreateCommand(string commandText, MySqlParameter[] parameters)
            {
                return this.CreateCommand(commandText, CommandType.Text, parameters);
            }        protected MySqlCommand CreateCommand(string commandText, CommandType commandType, MySqlParameter[] parameters)
            {
                return this.CreateCommand(this.DBConnection, commandType, commandText, 30, parameters);
            }        protected MySqlCommand CreateCommand(MySqlConnection connection, CommandType commandType, string commandText, int commandTimeout, MySqlParameter[] parameters)
            {
                MySqlCommand mySqlCommand;
                try
                {
                    using (this.mySqlCommand = new MySqlCommand())
                    {
                        if (connection.State != ConnectionState.Open)
                        {
                            connection.Open();
                        }
                        this.mySqlCommand.Connection = connection;
                        this.mySqlCommand.CommandType = commandType;
                        this.mySqlCommand.CommandText = commandText;
                        if ((parameters != null) && (parameters.Length > 0))
                        {
                            this.AddParameters(this.mySqlCommand, parameters);
                        }
                        this.mySqlCommand.CommandTimeout = commandTimeout;
                        mySqlCommand = this.mySqlCommand;
                    }
                }
                catch (MySqlException exception)
                {
                    throw new CustomException(exception.Message, exception);
                }
                return mySqlCommand;
            }        protected MySqlCommand CreateCommand(MySqlTransaction transaction, CommandType commandType, string commandText, int commandTimeout, MySqlParameter[] parameters)
            {
                MySqlCommand mySqlCommand;
                try
                {
                    using (this.mySqlCommand = new MySqlCommand())
                    {
                        if (transaction.Connection.State != ConnectionState.Open)
                        {
                            transaction.Connection.Open();
                        }
                        this.mySqlCommand.Connection = transaction.Connection;
                        this.mySqlCommand.CommandType = commandType;
                        this.mySqlCommand.CommandText = commandText;
                        if ((parameters != null) && (parameters.Length > 0))
                        {
                            this.AddParameters(this.mySqlCommand, parameters);
                        }
                        this.mySqlCommand.CommandTimeout = commandTimeout;
                        mySqlCommand = this.mySqlCommand;
                    }
                }
                catch (MySqlException exception)
                {
                    throw new CustomException(exception.Message, exception);
                }
                return mySqlCommand;
            }        protected MySqlCommand CreateCommandTransaction(string commandText)
            {
                return this.CreateCommandTransaction(commandText, CommandType.Text);
            }        protected MySqlCommand CreateCommandTransaction(string commandText, MySqlParameter[] parameters)
            {
                return this.CreateCommandTransaction(commandText, CommandType.Text, parameters);
            }        protected MySqlCommand CreateCommandTransaction(string commandText, CommandType commandType)
            {
                return this.CreateCommandTransaction(commandText, commandType, null);
            }        protected MySqlCommand CreateCommandTransaction(string commandText, CommandType commandType, MySqlParameter[] parameters)
            {
                return this.CreateCommand(this.DBTransaction, commandType, commandText, 30, parameters);
            }
      

  3.   

    public void Dispose()
            {
                if (this.DBConnection.State != ConnectionState.Closed)
                {
                    this.DBConnection.Dispose();
                }
                GC.Collect();
            }        internal int ExecuteNonQuery(string commandText)
            {
                int num;
                try
                {
                    num = this.CreateCommand(commandText).ExecuteNonQuery();
                }
                catch (MySqlException exception)
                {
                    throw new CustomException(exception.Message, exception);
                }
                return num;
            }        internal int ExecuteNonQuery(string commandText, MySqlParameter[] parameters)
            {
                int num;
                try
                {
                    num = this.CreateCommand(commandText, parameters).ExecuteNonQuery();
                }
                catch (MySqlException exception)
                {
                    throw new CustomException(exception.Message, exception);
                }
                return num;
            }        internal int ExecuteNonQueryProcedure(string commandText)
            {
                int num;
                try
                {
                    num = this.CreateCommand(commandText, CommandType.StoredProcedure).ExecuteNonQuery();
                }
                catch (MySqlException exception)
                {
                    throw new CustomException(exception.Message, exception);
                }
                return num;
            }        internal int ExecuteNonQueryProcedure(string commandText, MySqlParameter[] parameters)
            {
                int num;
                try
                {
                    num = this.CreateCommand(commandText, CommandType.StoredProcedure, parameters).ExecuteNonQuery();
                }
                catch (MySqlException exception)
                {
                    throw new CustomException(exception.Message, exception);
                }
                return num;
            }        internal int ExecuteNonQueryProcedureTransaction(string commandText)
            {
                int num;
                try
                {
                    num = this.CreateCommandTransaction(commandText, CommandType.StoredProcedure).ExecuteNonQuery();
                }
                catch (MySqlException exception)
                {
                    throw new CustomException(exception.Message, exception);
                }
                return num;
            }        internal int ExecuteNonQueryProcedureTransaction(string commandText, MySqlParameter[] parameters)
            {
                int num;
                try
                {
                    num = this.CreateCommandTransaction(commandText, CommandType.StoredProcedure, parameters).ExecuteNonQuery();
                }
                catch (MySqlException exception)
                {
                    throw new CustomException(exception.Message, exception);
                }
                return num;
            }        internal int ExecuteNonQueryTransaction(string commandText)
            {
                int num;
                try
                {
                    num = this.CreateCommandTransaction(commandText).ExecuteNonQuery();
                }
                catch (MySqlException exception)
                {
                    throw new CustomException(exception.Message, exception);
                }
                return num;
            }        internal int ExecuteNonQueryTransaction(string commandText, MySqlParameter[] parameters)
            {
                int num;
                try
                {
                    num = this.CreateCommandTransaction(commandText, parameters).ExecuteNonQuery();
                }
                catch (MySqlException exception)
                {
                    throw new CustomException(exception.Message, exception);
                }
                return num;
            }
      

  4.   

    这两段代码我只是大概的知道了一个框架下面那个dispose是重新写个类 那上面的那段代码是在我原有的地方加吗不知道怎么用,才接触这个一头雾水
      

  5.   

    发邮箱,我给你个类吧,代码多没有贴完。 我自己写的,或是你可以用 微软的 SqlHeler.cs 改下,