例外:已经有与此连接关联的开放的DataReader,必须先关闭 [英] Exception: There is already an open DataReader associated with this Connection which must be closed first

查看:5066
本文介绍了例外:已经有与此连接关联的开放的DataReader,必须先关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我有code和我得到异常:


  

有已经是公开的的DataReader 连接必须先关闭相关联。


我使用Visual Studio 2010 / .NET 4.0和MySQL这个项目。基本上,我试图同时使用数据读取器做我的其他任务运行另一个SQL语句。我正在逐渐线异常 cmdInserttblProductFrance.ExecuteNonQuery();

  SQL =SELECT * FROM tblProduct//创建连接/命令/了MySqlDataReader
的MySqlConnection MyConnection的=新的MySqlConnection(cf.GetConnectionString());
myConnection.Open();
的MySqlCommand myCommand =新的MySqlCommand(SQL,MyConnection的);
MySqlDataReader将myReader = myCommand.ExecuteReader();
myCommand.Dispose();如果(myReader.HasRows)
{
    INT I = 0;
    //访问数据之前,请务必调用Read。
    而(myReader.Read())
    {
        如果(myReader [frProductid]。的ToString()==)//没有存在的ProductID此项目
        {
            strInsertSQL =INSERT INTO tblProduct_temp(PRODUCTID)VALUES('这istest');
            的MySqlCommand cmdInserttblProductFrance =新的MySqlCommand(strInsertSQL,MyConnection的);
            cmdInserttblProductFrance.ExecuteNonQuery(); //< =====此行抛出C#的MySQL已经有与此连接,必须先关闭相关联的打开DataReader的。
        }
    }
}


解决方案

您正在使用相同的连接的DataReader 的ExecuteNonQuery 。这是不支持,根据MSDN


  

请注意,虽然一个DataReader是开放的,连接在使用
  完全由DataReader的。你不能执行任何命令的
  连接,其中包括创建另一个DataReader的,直到
  原来DataReader关闭。


I have below code and I am getting exception:

There is already an open DataReader associated with this Connection which must be closed first.

I am using Visual Studio 2010/.Net 4.0 and MySQL for this project. Basically I am trying to run another SQL statement while using data reader to do my other task. I am getting exception at line 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."
        }
    }
}

解决方案

You are using the same connection for the DataReader and the ExecuteNonQuery. This is not supported, according to MSDN:

Note that while a DataReader is open, the Connection is in use exclusively by that DataReader. You cannot execute any commands for the Connection, including creating another DataReader, until the original DataReader is closed.

这篇关于例外:已经有与此连接关联的开放的DataReader,必须先关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆