如何避免“;已经有一个打开的DataReader与此连接关联,必须先关闭它";在MySql/net连接器中
本文关键字:quot 连接器 net MySql 关联 有一个 何避免 连接 DataReader | 更新日期: 2023-09-27 17:58:31
我有以下代码:
public TimestampedRowStorage GetTimestampedRowStorage(string startTime, string endTime, long trendSettingID, int? period)
{
var timestampedList = (from t in dataContext.TrendRecords
where t.TrendSetting_ID == trendSettingID
select t).ToList();
return new TimestampedRowStorage
{
TimestampedDictionary = timestampedList.ToDictionary(m => m.Timestamp,
m => (from j in dataContext.TrendSignalRecords
where j.TrendRecord_ID == m.ID
select j).ToDictionary(p => p.TrendSignalSetting.Name,
p => (double?)p.Value))
};
}
但我总是得到以下例外:
已经有一个打开的DataReader与此连接关联必须先关闭。
这是堆栈跟踪:
[MySqlException(0x80004005):存在已关联一个打开的DataReader该连接必须先关闭。]
MySql。数据MySqlClient。MySqlCommand。CheckState()+237 MySql。数据MySqlClient。MySqlCommand。ExecuteReader(命令行为行为)+146
MySql。数据实体EFMySql命令。ExecuteDbDataReader(命令行为行为)+47
系统数据常见的DbCommand。ExecuteReader(命令行为行为)+10
系统数据EntityClient。EntityCommandDefinition。ExecuteStoreCommands(EntityCommand实体命令,命令行为行为)+443[EntityCommandExecutionException:执行时出错命令定义。查看内部详细信息除外。]
系统数据EntityClient。EntityCommandDefinition。ExecuteStoreCommands(EntityCommand实体命令,命令行为行为)+479
系统数据对象。内部的ObjectQueryExecutionPlan。执行(ObjectContext上下文,ObjectParameterCollectionparameterValues)+736
系统数据对象。对象查询1.GetResults(Nullable
1对于MergeOption)+149
系统数据对象。ObjectQuery1.Execute(MergeOption mergeOption) +31
1.Load(MergeOptionmergeOption)+148
System.Data.Objects.DataClasses.EntityReference
系统数据对象。DataClasses。RelatedEnd。加载()+37系统。数据对象。DataClasses。RelatedEnd。延迟加载()+8032198系统。数据对象。DataClasses。实体参考1.get_Value() +12 Nebula.Models.TrendSignalRecord.get_TrendSignalSetting() in C:'Users'Bruno Leonardo'documents'visual studio 2010'Projects'Nebula'Nebula'Models'SmgerDataModel.Designer.cs:2528 Nebula.Models.Trends.TrendRepository.<GetTimestampedRowStorage>b__b(TrendSignalRecord p) in C:'Users'Bruno Leonardo'documents'visual studio 2010'Projects'Nebula'Nebula'Models'Trends'TrendRepository.cs:229 System.Linq.Enumerable.ToDictionary(IEnumerable
1来源,Func2 keySelector, Func
2元素选择器,IEqualityComparer1 comparer) +226
1来源,Func
System.Linq.Enumerable.ToDictionary(IEnumerable2 keySelector, Func
2elementSelector)+54
星云模型。趋势。TrendRepository.b_a(趋势记录m) 在C:''Users''Bruno中Leonardo''documents''visualstudio2010''Projects''Nebula''Nebula''''Models''Trends''TrendRepository.cs:227系统林克。可枚举。ToDictionary(IEnumerable1 source, Func
2 keySelector,Func2 elementSelector, IEqualityComparer
1比较器)+240
系统林克。可枚举。ToDictionary(IEnumerable1 source, Func
2 keySelector,Func2 elementSelector) +53
1 period)C: ''用户''布鲁诺Leonardo''documents''visualstudio2010''Projects''Nebula''Nebula''''Models''Trends''TrendRepository.cs:224星云模型。趋势。TrendRepository。GetTrendSettingContainer(字符串startTime,字符串endTime,Int64unitID,Int64 plantID,国际64trendSettingID,GridSortOptionsgridSortOptions,可为Null的
Nebula.Models.Trends.TrendRepository.GetTimestampedRowStorage(String startTime, String endTime, Int64 trendSettingID, Nullable1 page, Nullable
1条记录PerPage,可为null的1 period, Int64[] trends, Nullable
1allTrends)在C:''Users''Bruno中Leonardo''documents''visualstudio2010''Projects''Nebula''Nebula''''Models''Trends''TrendRepository.cs:206星云控制器。GeneratingUnitController。趋势设置(Int64id,Int64 plantID,Int64trendSettingID,字符串startTime,字符串endTime,可为空的1 page, Nullable
1条记录PerPage,GridSortOptions选项,可为null的1 period, Int64[] trends, Nullable
1allTrends)在C:''Users''Bruno中Leonardo''documents''visualstudio2010''Projects''Nebula''Nebula''''Controllers''GeneratingUnitController.cs:148lambda_method(闭包,ControllerBase,Object[])+543
你们能帮我吗?
call.toList()
错误可能是因为您在访问数据库时试图访问数据库。您应该尝试将两个Linq表达式分开。也许可以放这样的东西:
var TimestampedList = (from t in dataContext.TrendRecords
where t.TrendSetting_ID == trendSettingID
select t).ToList();
TimestampedDictionary = timestampedList.ToDictionary(m => m.Timestamp,
m => (from j in dataContext.TrendSignalRecords
where j.TrendRecord_ID == m.ID
select j).ToDictionary(p => p.TrendSignalSetting.Name,
p => (double?)p.Value))
我认为MySql连接器不支持MARS(多个活动结果集)。在这种情况下,你不能这样做:
L2SQuery.ToDictionary(m => m.Timestamp, m => AnotherL2SQuery)
完成此操作后,您将枚举第一个L2S查询的结果(=DataReader
仍然打开),并对第一个记录中的每个记录执行第二个L2S询问(=您需要第二个DataReader
)。
您必须通过调用ToList
单独执行第一个查询,然后迭代结果并构建字典。
我今天也遇到了这个错误。我使用连接不止一件事。像这样:
MySqlConnection conn = new MySqlConnection(....);
conn.Open();
// Created a database here
// Populdate the database there
conn.Close();
但当我为每个动作打开和关闭连接时,它都起了作用:
MySqlConnection conn = new MySqlConnection(....);
conn.Open();
// Created a database here
conn.Close();
conn.Open();
// Populdate the database there
conn.Close();