Error “There is already an open DataReader associated with this Command which must be closed first” when using 2 distinct commands

后端 未结 7 1733
春和景丽
春和景丽 2020-11-29 01:23

I have this legacy code :

 private void conecta()
 {  
     if (conexao.State == ConnectionState.Closed)
         conexao.Open();
 }

 public List

        
7条回答
  •  我在风中等你
    2020-11-29 01:59

    You can get such a problem when you are two different commands on same connection - especially calling the second command in a loop. That is calling the second command for each record returned from the first command. If there are some 10,000 records returned by the first command, this issue will be more likely.

    I used to avoid such a scenario by making it as a single command.. The first command returns all the required data and load it into a DataTable.

    Note: MARS may be a solution - but it can be risky and many people dislike it.

    Reference

    1. What does "A severe error occurred on the current command. The results, if any, should be discarded." SQL Azure error mean?
    2. Linq-To-Sql and MARS woes - A severe error occurred on the current command. The results, if any, should be discarded
    3. Complex GROUP BY on DataTable

提交回复
热议问题