The SqlParameter is already contained by another SqlParameterCollection - Does using() {} cheat? The SqlParameter is already contained by another SqlParameterCollection - Does using() {} cheat? sql-server sql-server

The SqlParameter is already contained by another SqlParameterCollection - Does using() {} cheat?


I suspect that SqlParameter "knows" which command it's part of, and that that information isn't cleared when the command is disposed, but is cleared when you call command.Parameters.Clear().

Personally I think I'd avoid reusing the objects in the first place, but it's up to you :)


Using blocks do not ensure that an object is "destroyed", simply that the Dispose() method is called. What that actually does is up to the specific implementation and in this case it clearly does not empty the collection. The idea is to ensure that unmanaged resources that would not be cleaned up by the garbage collector are correctly disposed. As the Parameters collection is not an unmanaged resource it is not entirely suprising it is not cleared by the dispose method.


Adding cmd.Parameters.Clear(); after execution should be fine.