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

简介: 1.出现问题 场景1:在插入数据之前,先进行判断是否存在 执行SqlDataReader.Read之后,如果还想用另一个SqlCommand执行Insert或者Update操作的话, 会得到一个错误提示:There is already an open DataReader associated with this Command which must be closed first.   2.解决方法 在ConnectionString中加上一个参数“MultipleActiveResultSets”, 将其值设置为true。

1.出现问题

场景1:在插入数据之前,先进行判断是否存在

执行SqlDataReader.Read之后,如果还想用另一个SqlCommand执行Insert或者Update操作的话,

会得到一个错误提示:There is already an open DataReader associated with this Command which must be closed first.

 

2.解决方法

在ConnectionString中加上一个参数“MultipleActiveResultSets”, 将其值设置为true。

 

MultipleActiveResultSets可以使数据库连接复用。这样就不怕数据库的连接资源被耗尽了。使用方法很简单,只需要把它加到数据的连接字符串中即可。

例如:server=(local);Integrated Security = true;database=AdventureWorks;MultipleActiveResultSets=true;

相关文章
|
8月前
|
缓存 Windows
【cmd报错:Error :could not open】
【cmd报错:Error :could not open】
148 0
执行HQL直接被退出:Remote side unexpectedly closed network connection
执行HQL直接被退出:Remote side unexpectedly closed network connection
1079 0
执行HQL直接被退出:Remote side unexpectedly closed network connection
|
物联网 Linux 开发者
open_close 函数|学习笔记
快速学习 open_close 函数
109 0
|
网络安全 Windows
解决adb报错“failed to create fdevent interrupt socketpair: Invalid argument“问题
解决adb报错“failed to create fdevent interrupt socketpair: Invalid argument“问题
293 0
解决adb报错“failed to create fdevent interrupt socketpair: Invalid argument“问题
|
关系型数据库 MySQL 数据库
【mysql错误 Mac】ERR_WHEN_INSERT_TO_DB\nError:Connection lost: The server closed the connection.
ERR_WHEN_INSERT_TO_DB\nError:Connection lost: The server closed the connection.这个是数据库查询报出的错误,所以我们先登录一下数据库,查询看看具体报错
166 0
|
数据库连接 数据库管理
报错 XAConnectionFactory: failed to create pooled connection - DBMS down or unreachable?
报错 XAConnectionFactory: failed to create pooled connection - DBMS down or unreachable?
541 0
报错 XAConnectionFactory: failed to create pooled connection - DBMS down or unreachable?
|
SQL 存储 程序员
C#连接数据库之Connection、Command、DataReader用法总结
C#连接数据库之Connection、Command、DataReader用法总结
311 0
C#连接数据库之Connection、Command、DataReader用法总结
|
数据库
【QT】解决QSqlQuery::exec: database not open
【QT】解决QSqlQuery::exec: database not open
|
存储 SQL 数据库连接
浅谈ADO.NET中的对象——Connection、Command、DataReader、DataAdapter、DataSet、DataTable
可能是当初没有好好总结的缘故,学习.NET以来,对ADO.NET中的对象一直有些模糊,今天重新回顾了一下,通过查资料,总结,结合自己的观点整理一下ADO.NET中Connection、Command、DataReader、DataAdapter、
浅谈ADO.NET中的对象——Connection、Command、DataReader、DataAdapter、DataSet、DataTable