怎么解决的啊,
是不是每写一个SQL语句就要Close();
我被这个错误郁闷了一天了。
都不知道怎么写数据库类了。(ACCESS)

解决方案 »

  1.   

    每个连接都必须Close(),这是肯定的
      

  2.   

    你不要用datareader之类的需要数据库连接的对象,请使用datatable和dataset这样的可以关闭数据库连接的对象来读取数据,这样就可以直接闭掉连接了。public DataSet ExecuteQuery(string sql) 
    {
    IDbConnection con=null;
    IDbCommand dc=null;
    IDbDataAdapter da=null;
    DataSet ds=null;
    bool isError=false;
    try
    {
    con=this.GetDbConnection();
    dc=this.GetDbCommand(sql,con);
    da=this.GetDbDataAdapter(dc);
    ds=new DataSet();
    da.Fill(ds);
    return ds;
    }
    catch(AppException ex)
    {
    isError=true;
    throw ex;
    }
    catch(Exception ex)
    {
    isError=true;
    AppException MyException=new AppException("DB.ExecuteQuery()发生异常\r\n"+ex.ToString()+"\r\n"+sql);
    throw MyException;
    }
    finally
    {
    if(isError)
    {
    if(da!=null)
    {
    da=null;
    }
    if(ds!=null)
    {
    ds.Dispose();
    }
    }
    if(dc!=null)
    {
    dc.Dispose();
    }
    if(con!=null)
    {
    if(con.State!=ConnectionState.Closed)
    {
    con.Close();
    }
    con.Dispose();
    }
    }
    }
      

  3.   

    yczealot() ( ) 的建议很好
      

  4.   

    把你的数据访问层代码改的和微软写的一样就行了.比如SqlHelp之类的.你看下他们对应部分的代码.
      

  5.   

    用IDisposable来确保对象在超出作用域时释放数据库连接
      

  6.   

    我也明白了,
                    IDataReader dr = DataBase.GetReader("sql语句");
                    dr.Read();
                    lbl_UserRegTime.Text = dr[0].ToString()
    dr.Close();//这个必须要有因为在DataBase类里没有关闭
      

  7.   

    对于在线方式需要显式关闭(DataReader、Command)
    对于离线方式无效显式关闭(DataAdapter)
      

  8.   

    放着 using() 语法不用,用 try/finally, 真是吃多了
      

  9.   

    http://community.csdn.net/Expert/topic/4314/4314620.xml?temp=.2193109
      

  10.   

    楼上的,帖子我看了,主要还是我说的,
    IDataReader dr = DataBase.GetReader("sql语句");
    dr.Read();
    lbl_UserRegTime.Text = dr[0].ToString()
    dr.Close();//这个必须要有因为在DataBase类里没有关闭
      

  11.   

    http://www.verylead.cn/00Tools/DataBase.rar