Reputation: 51
Anyone can help? When i write this code and run. The program show me error stated "There is already an open DataReader associated with this Command which must be closed first".
This is my code.
Parallel.For(0, MthRange, i => {
PSUpfrontFeeForMonth[i] = CommissionSummary
.Where(s => s.TransDate == oReportCommonFilter.fromDate.AddMonths(i))
.Sum(s => s.PSUpfrontFee);
if (!PSUpfrontFeeForMonth[i].HasValue)
{
PSUpfrontFeeForMonth[i] = 0;
}
});
Thanks.
Regards, Jane
Upvotes: 4
Views: 1415
Reputation: 5358
Parallelizing database query is completely wrong for the following reasons:
Upvotes: 4
Reputation: 28646
If you really need to have multiple database connections open simultaneously (as others have stated not necessarily a good idea) then you can usually specify in the connection string that this is needed.
Typical scenario in which I've used this is for using a DataReader to stream rows from a database table (as I don't know how many rows I need in advanced) and where I then need to make additional queries on other database tables. I do this rather than a single query as it would require multiple complex joins and my app has a good caching layer to reduce queries to the database.
For Microsoft SQL Server you add MultipleActiveResultSets=True;
to the connection string
Upvotes: 1
Reputation: 39261
My guess would be something to do with how PSUpfrontFeeForMonth works internally is using data readers.
Since I have no idea how that works, first thing I would try would be to initialise the PSUpfrontFeeForMonth within the loop. Maybe that will ensure a dedicated data reader for each iteration.
Upvotes: 0