DataReader associated with this Command which must be closed first

三世轮回 提交于 2019-12-11 05:35:57

问题


I am getting the following error;

"There is already an open DataReader associated with this Command which must be closed first."

is it because I have used the reader in foreach loop ? or what the problem might be ? Regards BK

foreach( Apple a in listApple )
{


....
                   using (SmartSqlReader reader = Db.CurrentDb.ExecuteReader(sp))
                    {
                        while (reader.Read())
                        {
                            a.blablabla += reader.GetInt32("BLA_BLA_BLA"); 
                        }
                    }


.....

}

回答1:


Try the following:

using (SmartSqlReader reader = Db.CurrentDb.ExecuteReader(sp))
 {
  while (reader.Read())
  {
   a.blablabla += reader.GetInt32("BLA_BLA_BLA"); 
  }
  reader.Close();
 }



回答2:


Have you implemeneted the SmartSqlReader to close when it's disposed? The regular data readers implement the IDisposable interface and calls Close from the Dispose method.

If you don't close it properly it will keep the Command object occupied until the garbage collector will find the reader and clean it up.




回答3:


Add reader.Close() to close the SmartSqlReader



来源:https://stackoverflow.com/questions/1605504/datareader-associated-with-this-command-which-must-be-closed-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!