代码:
private static string _sqlConStr = "server=(local);database=ssjy;uid=sa;pwd=sa";
public static DataTable RunSQLGatDataTable(string sqlString)
    {
        DataTable dt = new DataTable();
        SqlConnection sqlConn = new SqlConnection(_sqlConStr);
        SqlCommand sqlComm = new SqlCommand(sqlString, sqlConn);
        SqlDataAdapter ada = new SqlDataAdapter(sqlComm);
        try
        {
            ada.Fill(dt);
        }
        catch (Exception exp)
        {
        }
        return dt;
    }
多次运行该代码,出现这样的问题:
{"超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。"}
Fill()方法好像是自动关闭Sql连接的吧?
这是什么原因呢?

解决方案 »

  1.   

    每次操作后,数据库连接要关闭
    finally
    {
    sqlConn.close();
    }
      

  2.   

    Fill()方法好像是自动关闭Sql连接的吧?
      

  3.   

    SqlDataAdapter不需要持续连接,但建议自己手写一下关闭连接
      

  4.   

    sqlconnection 的close 方法是可以允许多次调用的。所以使用连接就应该关闭。
    并且 连接池是透明的,sqlconnection.close 是还回到连接池的。程序从连接池取连接是资源消耗非常小的。
    SqlConnection sqlConnOne = new SqlConnection(_sqlConStr) ;
    sqlconnOne.open() 
    //看连接池中是否有 connectionString为 _sqlConStr空闲的连接,有取回连接。没有的,
    //重新创建一个新的连接并还回(注:如果现有的连接数大于等于 连接池 max connetion size,程序将等待,等待时间超过connectionTimeout还不能创建,系统抛出异常time Out 
    dosomething
    sqlconnOne.close()//还回到连接池
    SqlConnection sqlConnTwo = new SqlConnection(_sqlConStr) ;
    sqlconnTwo.open() 
    dosomething
    sqlconnTwo.close()//还回到连接池
    系统会连接池中对比 connectionString _sqlConStr,所以一般_sqlConstr一定要一致,这样才会有效的使用连接池。
    "server=(local);integrated security=sspi;database=name"
    "server=(local);database=name;integrated security=sspi"
    以上两个虽然含义一样,但是系统认为他们是不同的ConnectionString
      

  5.   

    解决:
    1、是要关闭所有连接
    2、我后面的有一个返回SqlDataReader 对象的方法在调用后没有关闭SqlDataReader 。