When looking at the proper way to query a database in C#, I often see this advise:
Make sure you dispose the Reader, Command, and Connection object
If I look at the documentation of SqlConnection, SqlCommand and SqlDataReader, all what I can see as description for Dispose method is
Releases all resources used by the Component.
I want to know what resources are used by these 3 types mentioned above. I thought only disposing SqlConnection will dispose and close other two nested objects(SqlCommand and SqlDataReader).
Also, there is many MSDN examples not disposing SqlCommand object. See
this example
So, I have two main concerns here:
I know that connection with database is unmanaged code and is not properly handled by garbage collector. I would like to know, what object or resources are used by each object and what will happen exactly if I don't dispose each one of them.
So PLEASE don't answer something general like unmanaged code, garbage collector, free resources...
This can change. The difference is they are unmanaged (think c instead of c#) and so we are basically just informing the unmanaged component it should release ANY resources it used... This includes closing open connections, freeing memory (as unmanaged code does not get the benefit of the garbage collector), closing handles it has requested from the operating system.
Memory leaks mostly. Half open connections on the database side. Failed code reviews and jibes from more experienced developers :P
Try read: https://msdn.microsoft.com/en-us/library/498928w2(v=vs.110).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With