下面是代码,但是最近老是:连接池已满,无法获取连接的问题不知道是不是这个类写的那不对???
public class DataBase
{
public DataBase()
{

}
    static string strcon = ConfigurationManager.AppSettings["ConnectionString"];
    public static int getExecuteNonQuery(string SQLString, SqlParameter[] paramer)
    {
        using (SqlConnection connection = new SqlConnection(strcon))
        {
            using (SqlCommand cmd = new SqlCommand(SQLString, connection))
            {
                try
                {
                    foreach (SqlParameter p in paramer)
                    {
                        cmd.Parameters.Add(p);
                    }
                    connection.Open();
                    int rows = cmd.ExecuteNonQuery();
                    return rows;
                }
                catch (System.Data.SqlClient.SqlException E)
                {
              
                    throw new Exception(E.Message);
                }
                finally {
                    connection.Close();
                }
            }
        }
    }
    public static int getExecuteNonQuery(string SQLString)
    {
        using (SqlConnection connection = new SqlConnection(strcon))
        {
            using (SqlCommand cmd = new SqlCommand(SQLString, connection))
            {
                try
                {
                    connection.Open();
                    int rows = cmd.ExecuteNonQuery();
                    return rows;
                }
                catch (System.Data.SqlClient.SqlException E)
                {
                    connection.Close();
                    throw new Exception(E.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
        }
    }
   
    public static DataSet getds(string strsql, SqlParameter[] paramer)
    {        using (SqlConnection connection = new SqlConnection(strcon))
        {
            using (SqlCommand cmd = new SqlCommand(strsql, connection))
            {
                DataSet ds = new DataSet();
                try
                {
                    foreach (SqlParameter p in paramer)
                    {
                        cmd.Parameters.Add(p);
                    }
                    connection.Open();
                    SqlDataAdapter command = new SqlDataAdapter(cmd);
                    command.Fill(ds, "ds");
                    return ds;
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    connection.Close();
                    throw new Exception(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
        }
    }
    public static DataSet getds(string SQLString)
    {
        using (SqlConnection connection = new SqlConnection(strcon))
        {
            DataSet ds = new DataSet();
            try
            {
                connection.Open();
                SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                command.Fill(ds, "ds");
                return ds;
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                connection.Close();
                throw new Exception(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            
        }
    }
    public static DataSet getds(string SQLString, int currentpage,int pagesize)
    {
        using (SqlConnection connection = new SqlConnection(strcon))
        {
            DataSet ds = new DataSet();
            try
            {
                connection.Open();
                SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                command.Fill(ds, pagesize * (currentpage - 1), pagesize, "ds"); 
                return ds;
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                connection.Close();
                throw new Exception(ex.Message);
            }
            finally
            {
                connection.Close();
            }
           
        }
    }
    public static DataSet getds(string SQLString, SqlParameter[] paramer, int currentpage, int pagesize)
    {        using (SqlConnection connection = new SqlConnection(strcon))
        {
            using (SqlCommand cmd = new SqlCommand(SQLString, connection))
            {
                DataSet ds = new DataSet();
                try
                {
                    foreach (SqlParameter p in paramer)
                    {
                        cmd.Parameters.Add(p);
                    }
                    connection.Open();
                    SqlDataAdapter command = new SqlDataAdapter(cmd);
                    command.Fill(ds, pagesize * (currentpage - 1), pagesize, "ds");
                    return ds;
                }
                catch (System.Data.SqlClient.SqlException E)
                {
                    connection.Close();
                    throw new Exception(E.Message);
                }
                finally
                {
                    connection.Close();
                }  
            }
        }
    }
    //执行存储过程
    public static void execproc()
    {
        using (SqlConnection connection = new SqlConnection(strcon))
        {            SqlCommand cmd = new SqlCommand();
                try
                {
                    cmd.Connection = connection;
                    cmd.CommandText = "getmidschid";
                    cmd.CommandType = CommandType.StoredProcedure;
                    connection.Open();
                    cmd.ExecuteNonQuery();
                    
                }
                catch (System.Data.SqlClient.SqlException E)
                {
                    connection.Close();
                    throw new Exception(E.Message);
                }
                finally
                {
                    connection.Close();
                }
            
        }
       
    }
   
   
}

解决方案 »

  1.   

    怎么你执行存储过程的时候,没有将传进来的变量赋值给每个存储过程中对应的变量
    cmd.Parameters.AddWithValue("@BookID", bookID);
      

  2.   

    而且你没有设置Command对象执行操作的类型。cmd.CommandType = CommandType.StoredProcedure;应该设置成执行存储过程StoredProcedure。
      

  3.   

    在 finally
                    {
                        connection.Close();
                        cmd.Dispose();
                    }
    其他没什么
      

  4.   

    楼主我贴下我的问题,大家也可以帮我解决一下,好吧
    http://topic.csdn.net/u/20090929/15/62ad727f-94b2-47ff-be8a-44667c51e574.html
      

  5.   

    finally里边这样写,if(Conn.State!=ConnectionState.Closed)
                            {
                                Conn.Close();
                            }
                            Conn.Dispose();
      

  6.   

    没用一个人提到using??
    不是吧
      

  7.   

    using有问题吗?如果有的话,那也是你return 的位置太靠前了
      

  8.   

    不太明白??
    要想using有作用的话,要把return放到using大括号的后面吗?