开发者社区> 问答> 正文

例外:已经有一个与此连接关联的打开的DataReader,必须先关闭它?mysql

我有下面的代码,我越来越异常:

已经有一个DataReader与此相关的开放Connection,必须首先关闭。

我为此项目使用Visual Studio 2010 / .Net 4.0和MySQL。基本上,我试图在使用数据读取器执行其他任务时运行另一个SQL语句。我在网上遇到异常cmdInserttblProductFrance.ExecuteNonQuery();

SQL = "Select * from tblProduct";

//Create Connection/Command/MySQLDataReader MySqlConnection myConnection = new MySqlConnection(cf.GetConnectionString()); myConnection.Open(); MySqlCommand myCommand = new MySqlCommand(SQL, myConnection); MySqlDataReader myReader = myCommand.ExecuteReader(); myCommand.Dispose();

if (myReader.HasRows) { int i = 0; // Always call Read before accessing data. while (myReader.Read()) { if (myReader["frProductid"].ToString() == "") //there is no productid exist for this item { strInsertSQL = "Insert Into tblProduct_temp (Productid) Values('this istest') "; MySqlCommand cmdInserttblProductFrance = new MySqlCommand(strInsertSQL, myConnection); cmdInserttblProductFrance.ExecuteNonQuery(); //<=====THIS LINE THROWS "C# mySQL There is already an open DataReader associated with this Connection which must be closed first." } } }

展开
收起
保持可爱mmm 2020-05-17 12:30:38 617 0
1 条回答
写回答
取消 提交回答
  • 您正在DataReader和和使用相同的连接ExecuteNonQuery。根据MSDN,这不受支持:

    请注意,在打开DataReader时,该DataReader仅使用Connection。在关闭原始DataReader之前,您无法执行任何用于Connection的命令,包括创建另一个DataReader。来源:stack overflow

    2020-05-17 12:40:11
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
One Box: 解读事务与分析一体化数据库 HybridDB for MySQL 立即下载
One Box:解读事务与分析一体化数据库HybridDB for MySQL 立即下载
如何支撑HTAP场景-HybridDB for MySQL系统架构和技术演进 立即下载

相关镜像