문제

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