.netmysqlsqlcommand

Do i need to dispose of MySqlCommand?


I find it incredibly annoying to write a using statement on every one of my queries (which require its own command or write parameters.clear()) which sometimes require declaring variables outside of the using block. Its so incredibly annoying and looks much dirtier compared to the version without disposing the object.

Do i need to dispose of it? what happens if i dont? I do know its good practice to dispose of an object when it has that interface.


Solution

  • From a quick glance in Reflector, it appears to call CloseStatement() on its PreparableStatement, and then call Dispose() on its superclasses, including System.ComponentModel.Component. Somewhere up the chain it ends up calling NativeDriver.CloseStatement32(id), which writes a command to the connection stream.

    It's not the sort of thing I'd want to skip. Why risk it?

    This is one of those times when you need to adjust your own thinking. You might think using looks "dirty", but -- like fresh oil glistening on a gear system -- it's actually a sign of cleanliness. You might be able to write an abstraction to hide it, but you shouldn't get rid of it.