What is meant by connection.Dispose()
in C#?
Connection. Dispose() will clean up completely, removing all unmanaged resources preventing that Connection from being used again. Once disposed is called you shouldn't try to use the object any more. Within Dispose(), Close()` will all most certainly be called too.
In the context of C#, dispose is an object method invoked to execute code required for memory cleanup and release and reset unmanaged resources, such as file handles and database connections.
Deletes it from the memory.
Connections are automatically pooled, and calling Dispose / Close on the connection does not physically close the connection (under normal circumstances).
Dispose() method permanently removes any resource ((un)managed) from memory for cleanup and the resource no longer exists for any further processing.
Example:-
void DataTest()
{
using(SqlConnection conn1 = new SqlConnection(...)) {
conn1.Open();
SqlCommand mycommand = new SqlCommand("Select * From someTable", conn1);
using(SqlDataReader myreader = mycommand.ExecuteReader()) {
if(myreader != null)
while(myreader.Read())
Console.WriteLine(myreader.GetValue(0).ToString() + ":" + myreader.GetTypeName(0));
}
mycommand.Dispose();
}
}
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