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?

Community
  • 1
  • 1
user1544004
  • 207
  • 4
  • 9

2 Answers2

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.

Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Does not answer the question though, because you don't mention whether or not `DataSet` implements it. – Tim Schmelter Aug 13 '12 at 15:56
  • @TimSchmelter - fair enough, answer updated. – Oded Aug 13 '12 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 '17 at 07: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

 });
sam1589914
  • 806
  • 4
  • 9