M
Mike
I am attempting to maintain a database connection across threads. I am
receiving the following exceptional error:
"There is already an open DataReader associated with this Connection
which must be closed first."
The code I use is in two stages: first, open the connection. Second,
execute the SQL statement. The code is as follows.
As you can see, I am not doing anything out of the ordinary. It
assumes the connection string has already been generated and accepted
by the nugget.
Could it be that the data reader hasn't closed yet? How might I manage
to wait for that event to occur?
SqlDataReader* DatabaseNugget::get_QueryExec(
String* strQueryText)
{
// Assumes the query is a properly formed SQL statement.
SqlConnection* conn = this->SqlConn;
if (conn->State == ConnectionState::Open)
{
this->m_sqlCmd = new SqlCommand(strQueryText, conn);
// Closes the data reader if it existed prior.
if (this->m_sqlDataReader)
this->m_sqlDataReader->Close();
this->m_sqlDataReader = this->m_sqlCmd->ExecuteReader();
this->m_strQueryText = strQueryText;
}
return this->m_sqlDataReader;
}
/////////////////////////////////////////////////////////////////////////////
SqlConnection* DatabaseNugget::get_SqlConn()
{
try
{
// Attempt to acquire a new database connection.
if (!this->m_sqlConn)
{
this->m_sqlConn = new SqlConnection(
this->m_strConnectionString);
}
ConnectionState state = this->m_sqlConn->State;
if (!(state == ConnectionState::Open
|| state == ConnectionState::Connecting
|| state == ConnectionState::Executing))
{
this->m_sqlConn->Open();
}
// Keep the connection alive.
GC::KeepAlive(this->m_sqlConn);
}
catch (Exception* except)
{
// Post the exception to the event viewer.
ResourceCenter::CenterFactory()->
EventThread->PostException(except);
}
return this->m_sqlConn;
}
/////////////////////////////////////////////////////////////////////////////
receiving the following exceptional error:
"There is already an open DataReader associated with this Connection
which must be closed first."
The code I use is in two stages: first, open the connection. Second,
execute the SQL statement. The code is as follows.
As you can see, I am not doing anything out of the ordinary. It
assumes the connection string has already been generated and accepted
by the nugget.
Could it be that the data reader hasn't closed yet? How might I manage
to wait for that event to occur?
SqlDataReader* DatabaseNugget::get_QueryExec(
String* strQueryText)
{
// Assumes the query is a properly formed SQL statement.
SqlConnection* conn = this->SqlConn;
if (conn->State == ConnectionState::Open)
{
this->m_sqlCmd = new SqlCommand(strQueryText, conn);
// Closes the data reader if it existed prior.
if (this->m_sqlDataReader)
this->m_sqlDataReader->Close();
this->m_sqlDataReader = this->m_sqlCmd->ExecuteReader();
this->m_strQueryText = strQueryText;
}
return this->m_sqlDataReader;
}
/////////////////////////////////////////////////////////////////////////////
SqlConnection* DatabaseNugget::get_SqlConn()
{
try
{
// Attempt to acquire a new database connection.
if (!this->m_sqlConn)
{
this->m_sqlConn = new SqlConnection(
this->m_strConnectionString);
}
ConnectionState state = this->m_sqlConn->State;
if (!(state == ConnectionState::Open
|| state == ConnectionState::Connecting
|| state == ConnectionState::Executing))
{
this->m_sqlConn->Open();
}
// Keep the connection alive.
GC::KeepAlive(this->m_sqlConn);
}
catch (Exception* except)
{
// Post the exception to the event viewer.
ResourceCenter::CenterFactory()->
EventThread->PostException(except);
}
return this->m_sqlConn;
}
/////////////////////////////////////////////////////////////////////////////