D
D. Yates
Is there anything wrong with immediately disposing of an OdbcCommand object
after calling its ExecuteReader method?
Yes, this example runs without error, but I'm concerned about a possible
timing issue in which garbage collection might destroy the OdbCommand object
when in fact it is still being referenced by the OdbcDataReader object (if
that's the case).
For example:
public static OdbcDataReader ExecuteReader(OdbcConnection aConnection,
string sSQL, UlSimpleMsgDelegate errorCallBack)
{
OdbcDataReader result;
try
{
OdbcCommand cmd = aConnection.CreateCommand();
try
{
cmd.CommandText = sSQL;
result = cmd.ExecuteReader();
}
finally
{
cmd.Dispose();
cmd = null;
}
}
catch(Exception e)
{
if (errorCallBack != null)
{
errorCallBack("Error Message : " + e.Message);
errorCallBack("SQL : " + sSQL);
errorCallBack("Stack Trace: " + e.StackTrace);
}
throw e;
}
return result;
}
Thanks,
Dave
after calling its ExecuteReader method?
Yes, this example runs without error, but I'm concerned about a possible
timing issue in which garbage collection might destroy the OdbCommand object
when in fact it is still being referenced by the OdbcDataReader object (if
that's the case).
For example:
public static OdbcDataReader ExecuteReader(OdbcConnection aConnection,
string sSQL, UlSimpleMsgDelegate errorCallBack)
{
OdbcDataReader result;
try
{
OdbcCommand cmd = aConnection.CreateCommand();
try
{
cmd.CommandText = sSQL;
result = cmd.ExecuteReader();
}
finally
{
cmd.Dispose();
cmd = null;
}
}
catch(Exception e)
{
if (errorCallBack != null)
{
errorCallBack("Error Message : " + e.Message);
errorCallBack("SQL : " + sSQL);
errorCallBack("Stack Trace: " + e.StackTrace);
}
throw e;
}
return result;
}
Thanks,
Dave