Search code examples
c#sql-server-cewindows-ce.net-1.0sqlcedatareader

Is disposing a SqlCeDataReader a good idea?


In trying to track down the cause of a Null Reference Exception which occurs only in certain situations, I came across this code:

SqlCeDataReader IdValsReader = IdValsCMD.ExecuteReader();
if ((IdValsReader.Read()) && (!IdValsReader.IsDBNull(0)))
{
    return string.Format("{0};{1};{2};{3};{4};{5};{6};{7};", 
        IdValsReader.GetValue(DESCRIPTION_INDEX),
        . . .
}
IdValsReader.Close();

...and wonder if I should add this line following the IdValsReader.Close():

IdValsReader.Dispose();

I did do so, but it seems to make no difference; should I leave it in or strip it out?


Solution

  • From MSDN: [The Dispose method] Releases the resources used by the DbDataReader and calls Close.

    So it seems sensible to call dispose once you're finished with the reader. Better still, wrap it all in a Using block and forget all about it.