C# – SQLDataReader + Multiple Result Set 方法

C# – SQLDataReader + Multiple Result Set 方法
 
Symptoms :
 
A SQLDataReader is iterated in a while loop.
The Other SQLDataReader from the function is called inside the while loop.
When it is started to be iterated, there is many Multiple SQLDataReader which is active.
Then the error would be occurred.
 
Error : Invalid attempt to call NextResult when reader is closed.
 
Solution : Update the Connection String as follow.
 
<add key="ConnStr" value="Data Source=localhost; Initial Catalog=Inventory; User id=INVAdm; Password=Passwd; MultipleActiveResultSets=true" />
 
It seems a old issue from C# Development & there is not only 1 Solution to solve this.

 
   private void ….()
   {
 
      … …
 
      SqlDataReader dr = cmd.ExecuteReader();
 
      if (dr.HasRows)
      {
         while (dr.Read())
         {
            … = getItem();
         }
      }
 
      … …
 
   }
 
   private void getItem()
   {
 
      … …
 
      SqlDataReader dr = cmd.ExecuteReader();
 
      if (dr.HasRows)
      {
         while (dr.Read())
         {
            … …
         }
      }
 
      … …
 
   }