it looks ok >>这样DataReader关闭了吗?no>>Connection关闭了吗?no>>如果用了好多这个make sure you call DataReader.Close() after you are done with the DataReader, it will automatically close the database connection since you used CommandBehavior.CloseConnectionThe general guideline with database connection is, open them as late as possible, and close them as soon as possible

解决方案 »

  1.   

    实际上,这种情况我觉得用DATASET生成DATAVIEW比较好,因为DATAREADER是非连接式的,他只是提供了一个命令通道,本身不保存命令产生的数据,所以,当连接关闭后,也就不能访问DATAREADER了,换句话来说,以上代码如果使用DATAREADER,那么,连接是不会立刻关闭的
      

  2.   

    PetShop中是这样的
    public void RunProc(string procName, out SqlDataReader dataReader) 
    {
    SqlCommand cmd = CreateCommand(procName, null);
    dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    }
      

  3.   

    但返回DataReader读了之后,一定要Close(), 这样Connection才会Close