Home >Database >Mysql Tutorial >Why Does `ExecuteNonQuery()` Throw \'There is already an open DataReader...\'?

Why Does `ExecuteNonQuery()` Throw \'There is already an open DataReader...\'?

Linda Hamilton
Linda HamiltonOriginal
2024-12-02 06:06:11306browse

Why Does `ExecuteNonQuery()` Throw

DataReader Open During Connection Execution

While using a Visual Studio 2010/.Net 4.0 and MySQL project, a developer encountered the exception, "There is already an open DataReader associated with this Connection which must be closed first."

The code in question attempts to execute another SQL statement while a data reader is open. Specifically, an exception is thrown at the line cmdInserttblProductFrance.ExecuteNonQuery();.

This issue arises because the connection being used for both the data reader and the ExecuteNonQuery command. As per MSDN, such usage is unsupported:

"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."

To resolve this, close the data reader before attempting to execute the additional SQL statement.

The above is the detailed content of Why Does `ExecuteNonQuery()` Throw \'There is already an open DataReader...\'?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn