已经有一个用此命令,必须先关闭相关联的打开的DataReader [英] There is already an open DataReader associated with this Command which must be closed first
问题描述
我有这个疑问,我得到这个函数的错误:
VAR应收帐=在context.Accounts
从account.Gurantors guranteer
选择新AccountsReport
{
CreditRegistryId = account.CreditRegistryId,
账户号码= account.AccountNo,
DateOpened = account.DateOpened,
}; 返回accounts.AsEnumerable()
。选择((账户,指数)=>新建AccountsReport()
{
RecordNumber = FormattedRowNumber(账户,指数+ 1),
CreditRegistryId = account.CreditRegistryId,
DateLastUpdated = DateLastUpdated(account.CreditRegistryId,account.AccountNumber)
账户号码= FormattedAccountNumber(account.AccountType,account.AccountNumber)
})
.OrderBy(C => c.FormattedRecordNumber)
.ThenByDescending(C => c.StateChangeDate);
公众的DateTime DateLastUpdated(长creditorRegistryId,串accountNo)
{
在context.AccountHistory返回(自H
其中,h.CreditorRegistryId == creditorRegistryId&放大器;&安培; h.AccountNo == accountNo
选择h.LastUpdated)。最大();
}
错误是:
有已经使用此命令,必须先关闭相关联的打开的DataReader。
块引用>更新:
堆栈跟踪又说:
InvalidOperationException异常:已经有一个用此命令,必须先关闭相关联的打开的DataReader]
System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(的SqlCommand命令)5008639
System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(字符串方法的SqlCommand命令)+23
System.Data.SqlClient.SqlCommand.ValidateCommand(字符串的方法,布尔异步)+144
System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,字符串的方法,DbAsyncResult结果)+87
System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,字符串方法)+32
System.Data.SqlClient.SqlCommand.ExecuteReader(的CommandBehavior行为,串法)+141
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(的CommandBehavior行为)+12
System.Data.Common.DbCommand.ExecuteReader(的CommandBehavior行为)+10
System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,行为的CommandBehavior)443[EntityCommandExecutionException:执行命令定义时出错。详情请参阅内部异常。]
System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,行为的CommandBehavior)479
System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext的背景下,ObjectParameterCollection的parameterValues)+683
System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)+119
System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable< T> .GetEnumerator()+38
System.Linq.Enumerable.Single(IEnumerable`1源)+114
System.Data.Objects.ELinq.ObjectQueryProvider< GetElementFunction> b__3(IEnumerable`1序列)+4
System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable`1查询,防爆pression queryRoot)+29
System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(前pression前pression)+91
System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(前pression前pression)+69
System.Linq.Queryable.Max(IQueryable`1源)+216
CreditRegistry.Repositories.CreditRegistryRepository.DateLastUpdated(Int64的creditorRegistryId,字符串accountNo)在D:\\自由职业者\\ SuperExpert \\ CreditRegistry \\ CreditRegistry \\库\\ CreditRegistryRepository.cs:1497
CreditRegistry.Repositories.CreditRegistryRepository< AccountDetails>在ðb__88(AccountsReport帐户的Int32指数):\\自由职业者\\ SuperExpert \\ CreditRegistry \\ CreditRegistry \\库\\ CreditRegistryRepository.cs:1250
System.Linq的< SelectIterator> d__7`2.MoveNext()+198
System.Linq.Buffer`1..ctor(IEnumerable`1源)+217
System.Linq的<&GetEnumerator的GT; d__0.MoveNext()+96
解决方案这可以,如果你在遍历从另一个查询的结果执行一个查询发生。它不是从你的例子不清楚的地方出现这种情况,因为这个例子是不完整的。
有一件事情,可能会导致这是迭代的一些查询的结果时,延迟加载触发。
这可以通过允许MARS在连接字符串中迎刃而解。添加
MultipleActiveResultSets = TRUE
来连接字符串(如指定数据源,初始目录等)的提供者的部分。I have this query and I get the error in this function:
var accounts = from account in context.Accounts from guranteer in account.Gurantors select new AccountsReport { CreditRegistryId = account.CreditRegistryId, AccountNumber = account.AccountNo, DateOpened = account.DateOpened, }; return accounts.AsEnumerable() .Select((account, index) => new AccountsReport() { RecordNumber = FormattedRowNumber(account, index + 1), CreditRegistryId = account.CreditRegistryId, DateLastUpdated = DateLastUpdated(account.CreditRegistryId, account.AccountNumber), AccountNumber = FormattedAccountNumber(account.AccountType, account.AccountNumber) }) .OrderBy(c=>c.FormattedRecordNumber) .ThenByDescending(c => c.StateChangeDate); public DateTime DateLastUpdated(long creditorRegistryId, string accountNo) { return (from h in context.AccountHistory where h.CreditorRegistryId == creditorRegistryId && h.AccountNo == accountNo select h.LastUpdated).Max(); }
Error is:
There is already an open DataReader associated with this Command which must be closed first.
Update:
stack trace added:
InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.] System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command) +5008639 System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command) +23 System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +144 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +87 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12 System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10 System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +443 [EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details.] System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +479 System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context, ObjectParameterCollection parameterValues) +683 System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +119 System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +38 System.Linq.Enumerable.Single(IEnumerable`1 source) +114 System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__3(IEnumerable`1 sequence) +4 System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable`1 query, Expression queryRoot) +29 System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +91 System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(Expression expression) +69 System.Linq.Queryable.Max(IQueryable`1 source) +216 CreditRegistry.Repositories.CreditRegistryRepository.DateLastUpdated(Int64 creditorRegistryId, String accountNo) in D:\Freelance Work\SuperExpert\CreditRegistry\CreditRegistry\Repositories\CreditRegistryRepository.cs:1497 CreditRegistry.Repositories.CreditRegistryRepository.<AccountDetails>b__88(AccountsReport account, Int32 index) in D:\Freelance Work\SuperExpert\CreditRegistry\CreditRegistry\Repositories\CreditRegistryRepository.cs:1250 System.Linq.<SelectIterator>d__7`2.MoveNext() +198 System.Linq.Buffer`1..ctor(IEnumerable`1 source) +217 System.Linq.<GetEnumerator>d__0.MoveNext() +96
解决方案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).这篇关于已经有一个用此命令,必须先关闭相关联的打开的DataReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!