4

Possible Duplicate:
Should I Dispose() DataSet and DataTable?

I am using a 3 tier architecture in my application,on Datalayer i am simple getting dataset

Dataset dset= new Dataset();

try 
{
    dset = SqlHelper.ExecuteDataset(Con, CommandType.StoredProcedure, "StoredProcedureName", arParms);
}
catch 
{}
finally 
{
    Con.Close();
    dset.Dispose()
}

Is there any performance benefit of disposing the data set object?

3
  • Same goes for Close(). If an object implements Dispose() or Close() you should use them.
    – Kevin
    Aug 13, 2012 at 15:52
  • @kevin Dispose is almost always an alternative to Close--you almost never need to do both. Aug 13, 2012 at 15:56
  • FYI: You don't need the empty catch block. You can just have a try-finally by itself. Aug 13, 2012 at 15:56

2 Answers 2

4

If an object implements IDisposable, you should dispose of it.

The best way to dispose of any object implementing IDisposable is to wrap the creation in a using statement:

using(var dset = SqlHelper.ExecuteDataset(Con, CommandType.StoredProcedure, 
                                                 "StoredProcedureName", arParms))
{
}

The above generates the correct disposal pattern for the created object. Using this pattern is a good rule of thumb - if you do it all the time, chances that you forget to dispose of something important are drastically lowered.


As Tim Schmelter commented, I did not address the issue of performance.

In the case of datasets, there will be no performance benefit, as disposal is suppressed in the constructor, as described in the answers to this SO question. At the same time, the overhead of calling dispose is minimal.

I suggest you test both approaches for your specific use case to see which one performs better and whether the benefits of using one option over the other are worth the downsides.

3
  • Does not answer the question though, because you don't mention whether or not DataSet implements it. Aug 13, 2012 at 15:56
  • @TimSchmelter - fair enough, answer updated.
    – Oded
    Aug 13, 2012 at 16:02
  • Just had to explain to a colleague why it makes absolutely no sense to complicate your application design just to allow for disposing of datasets (because now you have to make everything that has dataset members disposable itself) and he referenced this answer. Datasets suppress finalization and do nothing in their dispose method, so there's no point calling dispose. Sometimes you have to know when to break rules.
    – Voo
    Feb 21, 2017 at 7:14
0

You should dispose and close connections/objects having the IDisposable interface, there is most likley a benefit in performance as your program will use less resources and/or free them up faster.

You could make a new helper function which takes in an action, this way you don't have to think about clean up each time you use a dataset:

        public static void UseDataSet(Action<Dataset> code)
        {
            ...

            Dataset dset= new Dataset(); 

            try  
            { 
                dset = SqlHelper.ExecuteDataset(Con, CommandType.StoredProcedure, "StoredProcedureName", arParms); 
                code(dset);
            } 
            catch  
            {  } 
            finally  
            { 
               Con.Close(); 
               dset.Dispose() ;
            } 
        }

To use the function:

 Helper.UseDataSet( (dataset) =>
 {
    //use data set here, it will be closed and disposed afterwards
    dataset.something

 });

Not the answer you're looking for? Browse other questions tagged or ask your own question.