已经有一个与此命令关联的打开数据 Reader,必须先将其关闭 异常

There is already an open Data Reader associated with this Command which must be closed first Exception

我收到一个名为 There already an open Data Reader 与必须先关闭的命令关联的异常,我尝试在 Google 上查找解决方案我尝试使用 MARS=true在连接字符串中,并将所有内容都保留在 USING 中,但它并没有解决问题。 我得到一个例外 cm.ExecuteNonQuery();

public void UpdateActionSchedule(string actionScheduleKey, string note, string PEOPLE_CODE_ID)
{

    using (SqlConnection con = new SqlConnection("server=123; database=abc; user id=qwe; password=qwe;"))
    {
        con.Open();

        if (note == "" || note == null)
        {
            string UPDATE_COMPLETE = String.Format("UPDATE ACTIONSCHEDULE SET EXECUTION_DATE = '" + DateTime.Now + "', COMPLETED = 'Y', REVISION_OPID='WFLOW' where UNIQUE_KEY = '" + actionScheduleKey + "' and people_org_code_id='" + PEOPLE_CODE_ID + "'");
            SqlCommand cd = new SqlCommand(UPDATE_COMPLETE, con);
            cd.ExecuteNonQuery();
            cd.Dispose();
        }
        else
        {
            string oriNote = "";
            string GET_NOTE = String.Format("SELECT NOTE FROM ACTIONSCHEDULE WHERE people_org_code_id='{0}' and UNIQUE_KEY='{1}'", PEOPLE_CODE_ID, actionScheduleKey);
            using (SqlCommand cmd = new SqlCommand(GET_NOTE, con))
            {
                // SqlDataReader dr = cmd.ExecuteReader();
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            oriNote = dr["NOTE"].ToString();
                        }

                        note = oriNote + " " + note;
                    } 

                    //string UPDATE = String.Format("UPDATE ACTIONSCHEDULE SET Note = '" + note + "' where UNIQUE_KEY = '" + actionScheduleKey + "' and people_org_code_id='" + PEOPLE_CODE_ID + "'");
                    //SqlCommand cm = new SqlCommand(UPDATE, con);
                    //cm.ExecuteNonQuery();
                    //cm.Dispose();

                    string UPDATE_COMPLETE = String.Format("UPDATE ACTIONSCHEDULE SET EXECUTION_DATE = '" + DateTime.Now + "',Note = '" + note + "', COMPLETED = 'Y', REVISION_OPID='WFLOW' where UNIQUE_KEY = '" + actionScheduleKey + "' and people_org_code_id='" + PEOPLE_CODE_ID + "'");
                    SqlCommand cmw = new SqlCommand(UPDATE_COMPLETE, con);

                    cmw.ExecuteNonQuery();

                    cmw.Dispose();
                }
            }
        }
    }
}

在代码的后半部分,您在 cmd / dr 上有一个循环,并且 那个循环中,您使用 cmwExecuteNonQuery。这意味着您正试图同时执行两个命令。由于您已经完成了循环:只需将该代码 移到 之外 dr 上的 using

但是,看起来你也可以在单次往返中完成所有这些,而且 SQL。