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

前端 未结 18 2284
孤街浪徒
孤街浪徒 2020-11-22 01:40

I have this query and I get the error in this function:

var accounts = from account in context.Accounts
               from guranteer in account.Gurantors
           


        
相关标签:
18条回答
  • 2020-11-22 02:07

    I dont know whether this is duplicate answer or not. If it is I am sorry. I just want to let the needy know how I solved my issue using ToList().

    In my case I got same exception for below query.

    int id = adjustmentContext.InformationRequestOrderLinks.Where(
                 item => item.OrderNumber == irOrderLinkVO.OrderNumber 
                      && item.InformationRequestId == irOrderLinkVO.InformationRequestId)
                 .Max(item => item.Id);
    

    I solved like below

    List<Entities.InformationRequestOrderLink> links = 
          adjustmentContext.InformationRequestOrderLinks
               .Where(item => item.OrderNumber == irOrderLinkVO.OrderNumber 
                           && item.InformationRequestId == irOrderLinkVO.InformationRequestId)
               .ToList();
    
    int id = 0;
    
    if (links.Any())
    {
      id = links.Max(x => x.Id);
    }
    if (id == 0)
    {
    //do something here
    }
    
    0 讨论(0)
  • 2020-11-22 02:09

    Well for me it was my own bug. I was trying to run an INSERT using SqlCommand.executeReader() when I should have been using SqlCommand.ExecuteNonQuery(). It was opened and never closed, causing the error. Watch out for this oversight.

    0 讨论(0)
  • 2020-11-22 02:09

    I am using web service in my tool, where those service fetch the stored procedure. while more number of client tool fetches the web service, this problem arises. I have fixed by specifying the Synchronized attribute for those function fetches the stored procedure. now it is working fine, the error never showed up in my tool.

     [MethodImpl(MethodImplOptions.Synchronized)]
     public static List<t> MyDBFunction(string parameter1)
      {
      }
    

    This attribute allows to process one request at a time. so this solves the Issue.

    0 讨论(0)
  • 2020-11-22 02:10

    This can happen if you execute a query while iterating over the results from another query. It is not clear from your example where this happens because the example is not complete.

    One thing that can cause this is lazy loading triggered when iterating over the results of some query.

    This can be easily solved by allowing MARS in your connection string. Add MultipleActiveResultSets=true to the provider part of your connection string (where Data Source, Initial Catalog, etc. are specified).

    0 讨论(0)
  • 2020-11-22 02:16

    In my case, using Include() solved this error and depending on the situation can be a lot more efficient then issuing multiple queries when it can all be queried at once with a join.

    IEnumerable<User> users = db.Users.Include("Projects.Tasks.Messages");
    
    foreach (User user in users)
    {
        Console.WriteLine(user.Name);
        foreach (Project project in user.Projects)
        {
            Console.WriteLine("\t"+project.Name);
            foreach (Task task in project.Tasks)
            {
                Console.WriteLine("\t\t" + task.Subject);
                foreach (Message message in task.Messages)
                {
                    Console.WriteLine("\t\t\t" + message.Text);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:17

    For those finding this via Google;
    I was getting this error because, as suggested by the error, I failed to close a SqlDataReader prior to creating another on the same SqlCommand, mistakenly assuming that it would be garbage collected when leaving the method it was created in.

    I solved the issue by calling sqlDataReader.Close(); before creating the second reader.

    0 讨论(0)
提交回复
热议问题