【发布时间】:2011-07-23 08:21:05
【问题描述】:
我有以下代码,但遇到异常:
已经有一个打开的
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."
}
}
}
【问题讨论】:
-
我看到的错误信息是 '... 与这个 command 相关联...',我猜这个信息是错误的,这篇文章解释了