日期:2014-05-17  浏览次数:20566 次

已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭。
reader 和 connection 都关闭了,为什么还会出现上面错误呢(如题)

前台:

HTML code

    <ul id="menu" runat="server">
    
    </ul>



后台:

C# code

protected string sConnString = "...";   // 连接数据库字符串
protected void Page_Load(object sender, EventArgs e)
{
      menu.InnerHtml = CreateMenuHtml();        
}

public string CreateMenuHtml()
{
      StringBuilder sMenuHtml = new StringBuilder();
      if(isPurview("itemA")) { sMenuHtml.append(...) ......}
      if(isPurview("itemB")) { sMenuHtml.append(...) ......}
      if(isPurview("itemC")) { sMenuHtml.append(...) ......}
      if(isPurview("itemD")) { sMenuHtml.append(...) ......}
      if(GetOther("@ABlock", "ABlock")==1) { sMenuHtml.append(...) ......} // 添上了这句就出现如题的错误。 
      return sMenuHtml.ToString();
}

//
public bool isPurview(string item)
{
            SqlConnection connection = new SqlConnection(this.sConnString);
            SqlCommand command = new SqlCommand("IS_Purview", connection);
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter username = command.Parameters.Add("@sUserName", SqlDbType.Char, 50);
            SqlParameter purview = command.Parameters.Add("@sPurview", SqlDbType.Char, 50);
            SqlParameter intReturn = command.Parameters.Add("rv", SqlDbType.Int);
            username.Direction = ParameterDirection.Input;
            purview.Direction = ParameterDirection.Input;
            intReturn.Direction = ParameterDirection.ReturnValue;
            username.Value = Session["username"].ToString();
            purview.Value = item;
            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }            
            SqlDataReader reader = command.ExecuteReader();
            int returnValue = int.Parse(command.Parameters["rv"].Value.ToString());
            reader.Close();
            connection.Close();
            if (returnValue == 1)
                return true;
            else
                return false; 
}  

public int GetOther(string item, string itemvalue)
{
            int iOther=0;
            SqlConnection connection = new SqlConnection(this.sConnString);
            SqlCommand command = new SqlCommand("IS_Other", connection);
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter OtherList = command.Parameters.Add(item, SqlDbType.Char, 50);
            OtherList.Direction = ParameterDirection.Input;
            OtherList.Value = itemvalue;
            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }
            SqlDataReader reader = command.ExecuteReader();
            reader = command.ExecuteReader();
            if (reader.Read())
            {
                iOther = Convert.ToInt32(reader[0]);
            }

            reader.Close();
            connection.Close();
            return iOther;
}



------解决方案--------------------
C# code




加上try..catch..finally,比如:

public bool isPurview(string item)
{
            int returnValue=0;
            SqlConnection connection = new SqlConnection(this.sConnString);
            SqlDataReader reader =null;
            SqlCommand command = new SqlCommand("IS_Purview", connection);
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter username = command.Parameters.Add("@sUserName", Sq