Should I Dispose() DataSet and DataTable?

前端 未结 12 1497
清酒与你
清酒与你 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 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;
            }
        }
    }
    

提交回复
热议问题