public static SqlDataReader QueryReader(string strSQL)
                    {
                        SqlConnection connection = new SqlConnection(connectionString);
                        //{
                            SqlCommand cmd = new SqlCommand(strSQL, connection);
                            try
                            {//执行数据库命令
                                DoConn(connection);
                                SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);   
                                return myReader;
                            }
                            catch (Exception e)
                            {//处理异常
                                throw new Exception(e.Message);
                            }
                            finally
                            {//释放资源
                                cmd.Dispose();
                                //connection.Close();
                            }
                        //}                    }调用这个方法返回一个datareader页面datareader.close()的时候,连接关闭了吗??是不是真的关闭了啊?如何看啊?

解决方案 »

  1.   

    这个是引用传递,调用close肯定能关闭
      

  2.   

    可是我把connection.Close();注释了啊,只加了一个CommandBehavior.CloseConnection这个东西可靠吧???
      

  3.   

    可是我把connection.Close();注释了啊,有人回答吗
      

  4.   

    你把close注释了你说那执行吗?
      

  5.   

    如果在这句之前发送了异常就关闭不了SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); 如果没有发生异常就可以关闭...
      

  6.   


    cmd.Dispose();请问这个是在sqldatareader.close() 之后执行的吗?
      

  7.   

    如何看啊?
    -----------------
    你打出来看:
     if (connection != null)
                    MessageBox.Show(connection.State.ToString());//closed 关闭,open 打开
      

  8.   


    我的页面里没有CONNECTION啊,怎么打啊,在SQLHELPER里啊
      

  9.   

    在这里不是有吗,在这了加啊
    public static SqlDataReader QueryReader(string strSQL)
      {
      SqlConnection connection = new SqlConnection(connectionString);
      //{
      SqlCommand cmd = new SqlCommand(strSQL, connection);
      try
      {//执行数据库命令
      DoConn(connection);
      SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); 
      myReader.Read();
      myReader.close();
      if (connection != null)
      MessageBox.Show(connection.State.ToString());
      return myReader;
      }
      catch (Exception e)
      {//处理异常
      throw new Exception(e.Message);
      }
      finally
      {//释放资源
      cmd.Dispose();
      //connection.Close();
      }
      //}