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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top