S
Sean Wolfe
I haven't received an answer on this so I'm reposting.
I have searched through the newsgroups on an exception I am having with
SqlCommand.ExecuteNonQuery() being used asynchronously in a Thread Pool. But
every problems seems to cover the issue of using an existing connection and
using the same connection for execution on 2 or more separate threads. But
in my method, I create a new connection for each Execution.
I did read also that there was a bug in the 1.0 framework that was later
fixed with a service pack. But I'm using the 1.1 frameworks. Did this bug
make it back into the 1.1 framework?
This seems to be a bug since I am using a new connection for each reader. Or
is there a problem with the Connection pool?
Here is an example of the method that is called to execute the object.
Basically a higher up method iterates through the collection and then Calls
this method per object using the ThreadPool class. a threadCallState object
holds the state data to run the execution. This method build a parameter
list from other utility methods, creates a new connection, Executes the
query, then closes and disposes the connection. But I still get the "There
is already an open DataReader associated with this Connection which must be
closed first."
private void ExecuteCommandOnObject(object state)
{
threadCallState callState = (threadCallState)state;
SqlCommand command = callState.command;
object commandDataItem = callState.commandDataItem;
ListDictionary persistentParams = callState.persistentParams;
SqlConnection executeConnection = new
SqlConnection(this.sqlConnectionString);
//Open a database connection and Fill the table with the data.
command.Connection = executeConnection;
command.Connection.Open();
int parametersCount = command.Parameters.Count;
for(int paramIndex = 0; paramIndex < parametersCount; paramIndex++)
{
SqlParameter parameter = command.Parameters[paramIndex];
string paramName = parameter.ParameterName;
// get the property and put it into the parameter.
if(persistentParams.Contains(paramName))
parameter.Value = persistentParams[paramName];
else
{
paramName = paramName.Replace("@", "");
System.Reflection.PropertyInfo property =
this.propertyArray[this.dataMap.GetIndexOfColumnName(paramName)];
parameter.Value = property.GetValue(commandDataItem, null ); //
Will not work on indexed properties.
}
}
// Execute the query, then close the connection.
command.ExecuteNonQuery();
executeConnection.Close();
executeConnection.Dispose();
executeConnection = null;
}
Thanks,
sean
I have searched through the newsgroups on an exception I am having with
SqlCommand.ExecuteNonQuery() being used asynchronously in a Thread Pool. But
every problems seems to cover the issue of using an existing connection and
using the same connection for execution on 2 or more separate threads. But
in my method, I create a new connection for each Execution.
I did read also that there was a bug in the 1.0 framework that was later
fixed with a service pack. But I'm using the 1.1 frameworks. Did this bug
make it back into the 1.1 framework?
This seems to be a bug since I am using a new connection for each reader. Or
is there a problem with the Connection pool?
Here is an example of the method that is called to execute the object.
Basically a higher up method iterates through the collection and then Calls
this method per object using the ThreadPool class. a threadCallState object
holds the state data to run the execution. This method build a parameter
list from other utility methods, creates a new connection, Executes the
query, then closes and disposes the connection. But I still get the "There
is already an open DataReader associated with this Connection which must be
closed first."
private void ExecuteCommandOnObject(object state)
{
threadCallState callState = (threadCallState)state;
SqlCommand command = callState.command;
object commandDataItem = callState.commandDataItem;
ListDictionary persistentParams = callState.persistentParams;
SqlConnection executeConnection = new
SqlConnection(this.sqlConnectionString);
//Open a database connection and Fill the table with the data.
command.Connection = executeConnection;
command.Connection.Open();
int parametersCount = command.Parameters.Count;
for(int paramIndex = 0; paramIndex < parametersCount; paramIndex++)
{
SqlParameter parameter = command.Parameters[paramIndex];
string paramName = parameter.ParameterName;
// get the property and put it into the parameter.
if(persistentParams.Contains(paramName))
parameter.Value = persistentParams[paramName];
else
{
paramName = paramName.Replace("@", "");
System.Reflection.PropertyInfo property =
this.propertyArray[this.dataMap.GetIndexOfColumnName(paramName)];
parameter.Value = property.GetValue(commandDataItem, null ); //
Will not work on indexed properties.
}
}
// Execute the query, then close the connection.
command.ExecuteNonQuery();
executeConnection.Close();
executeConnection.Dispose();
executeConnection = null;
}
Thanks,
sean