Disposing of an SQL dataset I am getting a compiler warning when running my code analysis.  It is CA2202.  It is saying that I have not properly disposed my dataset through all exception paths.  Here is the code:

    private System.Data.DataSet MyDataSet()
    {
        DataSet dat_set = new DataSet(); 

        using (SqlConnection conn = new SqlConnection(conn_string))
        {
            try
            {
                da_1 = new SqlDataAdapter(sql_string, conn);
                dat_set.Locale = CultureInfo.InvariantCulture;
                da_1.Fill(dat_set, "Table_Data_1");
                MainForm.wasSuccess = true;
            }
            catch (SqlException ex)
            {
                Console.Write(ex.ToString());
            }
        }

        return dat_set;
    }


Any thoughts on how to properly dispose this object?
You don't want to dispose it. Otherwise your client code will be working off a disposed object and you'll be getting ObjectDisposedException's left and right.

The question is, does your client code really need a DataSet? or does it simply need a representation of the data?

If it actually needs to be working off a DataSet object, you can ignore the warning, as long as your client code is properly disposing it when it's done working with it.

If all you need is a representation of the data, then the solution is to not return a DataSet, but some IEnumerable&lt;Foo&gt; or IReadOnlyList&lt;Foo&gt;, where Foo is a Plain Old CLR Object (POCO - a simple class exposing nothing more than property getters and setters).

Instead of using a DataSet, you could use a SqlDataReader, which you obtain by executing a SqlCommand - note that returning an IEnumerable&lt;T&gt; makes it possible to yield return your results:

public IEnumerable&lt;Foo&gt; GetData(string connectionString, string sqlStatement)
{
    using (var connection = new SqlConnection(connectionString)) 
    using(var command = new SqlCommand(sqlStatement, connection)) 
    {
       connection.open(); 
       using (var reader = command.ExecuteReader())
       { 
            while (reader.Read()) 
            { 
               yield return new Foo
                   {
                       Bar = (int)reader[0], // retrieve by field index, or..
                       Something = (string)reader["SomeField"] // retrieve by field name
                   };
            } 
       } 
    }
}




Couple of other points:


Don't use underscores in identifiers. Use the more standard camelCase instead.
Don't abbreviate or disemvowel identifiers. There's no reason to do this.
Don't update the UI in a method whose responsibility is to retrieve data.
If you don't know what to do with an exception, let it bubble up, the client code is more likely to know what to do (e.g. pop a messagebox, or show a meaningful error message in the UI).
Use objects. The instruction MainForm.wasSuccess = true; looks very very much like you're accessing a static member, which is bad because static things belong to a type, as opposed to an instance.