본문 바로가기
MySql

MySQL 예외 : 먼저 닫아야하는이 연결과 연결된 열린 DataReader가 이미 있습니다.

by 베이스 공부 2020. 10. 13.
반응형

아래 코드가 있는데 예외가 발생합니다.

먼저 닫아야하는이 Connection 과 관련된 열린 DataReader 가 이미 있습니다.

이 프로젝트에 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."
        }
    }
}

 

해결 방법

 


DataReader가 열려있는 동안 Connection이 사용 중입니다. exclusively by that DataReader. You cannot execute any commands for the Connection, including creating another DataReader, until the 원래 DataReader가 닫힙니다.


 

참조 페이지 https://stackoverflow.com/questions/5440168

 

 

반응형

댓글