Is there a difference between DataTable.Clear and DataTable.Rows.Clear?

后端 未结 7 1567
天涯浪人
天涯浪人 2021-01-18 06:51

I recall there is a difference between some methods/properties called directly on the DataTable class, and the identically named methods/properties on the DataTable.Rows pro

7条回答
  •  无人及你
    2021-01-18 07:25

    In .Net 1.1, DataRowCollection.Clear calls DataTable.Clear

    However, in .Net 2.0, there is a difference. If I understand the source correctly, DataTable.Clear will clear unattached rows (created using DataTable.NewRow) whereas DataRowCollection.Clear won't.

    The difference is in RecordManager.Clear (source below, from the .Net Reference Source for v3.5 SP 0); clearAll is true only when called from DataTable.Clear.

        internal void Clear(bool clearAll) { 
            if (clearAll) {
                for(int record = 0; record < recordCapacity; ++record) { 
                    rows[record] = null;
                }
                int count = table.columnCollection.Count;
                for(int i = 0; i < count; ++i) { 
                    //
    
                    DataColumn column = table.columnCollection[i]; 
                    for(int record = 0; record < recordCapacity; ++record) {
                        column.FreeRecord(record); 
                    }
                }
                lastFreeRecord = 0;
                freeRecordList.Clear(); 
            }
            else { // just clear attached rows 
                freeRecordList.Capacity = freeRecordList.Count + table.Rows.Count; 
                for(int record = 0; record < recordCapacity; ++record) {
                    if (rows[record]!= null && rows[record].rowID != -1) { 
                        int tempRecord = record;
                        FreeRecord(ref tempRecord);
                    }
                } 
            }
        } 
    

提交回复
热议问题