The using statement automatically executes the Dispose method contained in IDisposable, the database related classes (for example, SqlConnection, SqlCommand, etc.) implement this interface.
So, if I am going to use such classes, should I use the using statement to create the objects so that the resources are freed when the operation completes?
For example, for some reason I need to use SqlConnection, SqlCommand, SqlDataAdapter and DataTable, so I write this code below, is this the best way to do this, or should I put Dispose () in the finally clause try ... catch ... finally?
using (SqlConnection con = new SqlConnection(Properties.Settings.Default.ConnectionString)) using (SqlCommand cmd = new SqlCommand()) using (SqlDataAdapter da = new SqlDataAdapter()) using (DataTable dt = new DataTable()) {
Zignd source share