I was given the sample C# code below (I may have changed it a bit) as an example for executing a stored procedure. I was trying different variants of the code (putting ".close" and ".dispose" everywhere) and then running the Visual Studio "Code Analysis" tool --- I keep getting warnings like "In method 'GETALL(ref DataTable)', call System.IDisposable.Dispose on object 'myCmnd' before all references to it are out of scope
I get the same warnings for the Connection, the Command, the Reader, and the Adapter.
What would be the prefered method for using "close" and "dispose" in an example like this?
// value of string "conn" set elsewhere ...
public string GETALL(ref DataTable myDataTable)
SqlConnection myConn = new SqlConnection(conn);
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlCommand myCmnd = new SqlCommand("GETALL_CUST", myConn);
myCmnd.CommandType = CommandType.StoredProcedure;
SqlDataReader myReader = myCmnd.ExecuteReader();
catch (SqlException ex)
MessageBox.Show("Some bad happened");
return "Didn't work"
return "it worked";