Should I Dispose() DataSet and DataTable?

前端 未结 12 1357
清酒与你
清酒与你 2020-11-22 02:45

DataSet and DataTable both implement IDisposable, so, by conventional best practices, I should call their Dispose() methods.

However, from what I\'ve read so far, Da

相关标签:
12条回答
  • 2020-11-22 02:55

    Here are a couple of discussions explaining why Dispose is not necessary for a DataSet.

    To Dispose or Not to Dispose ?:

    The Dispose method in DataSet exists ONLY because of side effect of inheritance-- in other words, it doesn't actually do anything useful in the finalization.

    Should Dispose be called on DataTable and DataSet objects? includes some explanation from an MVP:

    The system.data namespace (ADONET) does not contain unmanaged resources. Therefore there is no need to dispose any of those as long as you have not added yourself something special to it.

    Understanding the Dispose method and datasets? has a with comment from authority Scott Allen:

    In pratice we rarely Dispose a DataSet because it offers little benefit"

    So, the consensus there is that there is currently no good reason to call Dispose on a DataSet.

    0 讨论(0)
  • 2020-11-22 02:59

    And this can be the best/proper way to Dispose and release the memory consumed by DataSet.

    try
        {
            DataSet ds = new DataSet("DS");
            //use table DataTable here
            
        }
        catch {  }
        finally
        {
            if (ds != null)
                    {
                        ds.EnforceConstraints = false;
                        ds.Relations.Clear();
                        int totalCount = ds.Tables.Count;
    
                        for (int i = totalCount - 1; i >= 0; i--)
                        {
                            DataTable td1 = ds.Tables[i];
                            if (td1 != null)
                            {
                                td1.Constraints.Clear();
                                td1.Clear();
                                td1.Dispose();
                                td1 = null;
                            }
                        }
    
                        ds.Tables.Clear();
                        ds.Dispose();
                        ds = null;
                    }
        }
    
    0 讨论(0)
  • 2020-11-22 03:01

    Try to use Clear() function. It works great for me for disposing.

    DataTable dt = GetDataSchema();
    //populate dt, do whatever...
    dt.Clear();
    
    0 讨论(0)
  • 2020-11-22 03:03

    No need to Dispose() because DataSet inherit MarshalByValueComponent class and MarshalByValueComponent implement IDisposable Interface

    0 讨论(0)
  • 2020-11-22 03:06

    This is the right way to properly Dispose the DataTable.

    private DataTable CreateSchema_Table()
    {
        DataTable td = null;
        try
        {
            td = new DataTable();
            //use table DataTable here
            
            return td.Copy();
        }
        catch {  }
        finally
        {
            if (td != null)
            {
                td.Constraints.Clear();
                td.Clear();
                td.Dispose();
                td = null;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:13

    I call dispose anytime an object implements IDisposeable. It's there for a reason.

    DataSets can be huge memory hogs. The sooner they can be marked for clean up, the better.

    update

    It's been 5 years since I answered this question. I still agree with my answer. If there is a dispose method, it should be called when you are done with the object. The IDispose interface was implemented for a reason.

    0 讨论(0)
提交回复
热议问题