System.Data.DataTable.CloneTo C# (CSharp) Method

CloneTo() private method

private CloneTo ( DataTable clone, DataSet cloneDS, bool skipExpressionColumns ) : DataTable
clone DataTable
cloneDS DataSet
skipExpressionColumns bool
return DataTable
        private DataTable CloneTo(DataTable clone, DataSet cloneDS, bool skipExpressionColumns)
        {
            // we do clone datatables while we do readxmlschema, so we do not want to clone columnexpressions if we call this from ReadXmlSchema
            // it will cause exception to be thrown in cae expression refers to a table that is not in hirerachy or not created yet
            Debug.Assert(clone != null, "The table passed in has to be newly created empty DataTable.");

            // set All properties
            clone._tableName = _tableName;

            clone._tableNamespace = _tableNamespace;
            clone._tablePrefix = _tablePrefix;
            clone._fNestedInDataset = _fNestedInDataset;

            clone._culture = _culture;
            clone._cultureUserSet = _cultureUserSet;
            clone._compareInfo = _compareInfo;
            clone._compareFlags = _compareFlags;
            clone._formatProvider = _formatProvider;
            clone._hashCodeProvider = _hashCodeProvider;
            clone._caseSensitive = _caseSensitive;
            clone._caseSensitiveUserSet = _caseSensitiveUserSet;

            clone._displayExpression = _displayExpression;
            clone._typeName = _typeName; //enzol
            clone._repeatableElement = _repeatableElement; //enzol
            clone.MinimumCapacity = MinimumCapacity;
            clone.RemotingFormat = RemotingFormat;

            // add all columns
            DataColumnCollection clmns = Columns;
            for (int i = 0; i < clmns.Count; i++)
            {
                clone.Columns.Add(clmns[i].Clone());
            }

            // add all expressions if Clone is invoked only on DataTable otherwise DataSet.Clone will assign expressions after creating all relationships.
            if (!skipExpressionColumns && cloneDS == null)
            {
                for (int i = 0; i < clmns.Count; i++)
                {
                    clone.Columns[clmns[i].ColumnName].Expression = clmns[i].Expression;
                }
            }

            // Create PrimaryKey
            DataColumn[] pkey = PrimaryKey;
            if (pkey.Length > 0)
            {
                DataColumn[] key = new DataColumn[pkey.Length];
                for (int i = 0; i < pkey.Length; i++)
                {
                    key[i] = clone.Columns[pkey[i].Ordinal];
                }
                clone.PrimaryKey = key;
            }

            // now clone all unique constraints
            // Rename first
            for (int j = 0; j < Constraints.Count; j++)
            {
                ForeignKeyConstraint foreign = Constraints[j] as ForeignKeyConstraint;
                UniqueConstraint unique = Constraints[j] as UniqueConstraint;
                if (foreign != null)
                {
                    if (foreign.Table == foreign.RelatedTable)
                    {
                        ForeignKeyConstraint clonedConstraint = foreign.Clone(clone);
                        Constraint oldConstraint = clone.Constraints.FindConstraint(clonedConstraint);
                        if (oldConstraint != null)
                        {
                            oldConstraint.ConstraintName = Constraints[j].ConstraintName;
                        }
                    }
                }
                else if (unique != null)
                {
                    UniqueConstraint clonedConstraint = unique.Clone(clone);
                    Constraint oldConstraint = clone.Constraints.FindConstraint(clonedConstraint);
                    if (oldConstraint != null)
                    {
                        oldConstraint.ConstraintName = Constraints[j].ConstraintName;
                        foreach (object key in clonedConstraint.ExtendedProperties.Keys)
                        {
                            oldConstraint.ExtendedProperties[key] = clonedConstraint.ExtendedProperties[key];
                        }
                    }
                }
            }

            // then add
            for (int j = 0; j < Constraints.Count; j++)
            {
                if (!clone.Constraints.Contains(Constraints[j].ConstraintName, true))
                {
                    ForeignKeyConstraint foreign = Constraints[j] as ForeignKeyConstraint;
                    UniqueConstraint unique = Constraints[j] as UniqueConstraint;
                    if (foreign != null)
                    {
                        if (foreign.Table == foreign.RelatedTable)
                        {
                            ForeignKeyConstraint newforeign = foreign.Clone(clone);
                            if (newforeign != null)
                            { // we cant make sure that we recieve a cloned FKC,since it depends if table and relatedtable be the same
                                clone.Constraints.Add(newforeign);
                            }
                        }
                    }
                    else if (unique != null)
                    {
                        clone.Constraints.Add(unique.Clone(clone));
                    }
                }
            }

            // ...Extended Properties...

            if (_extendedProperties != null)
            {
                foreach (object key in _extendedProperties.Keys)
                {
                    clone.ExtendedProperties[key] = _extendedProperties[key];
                }
            }

            return clone;
        }

Usage Example

        private DataTable CloneHierarchy (DataTable sourceTable, DataSet ds, Hashtable visitedMap) {
            if (visitedMap == null)
                visitedMap = new Hashtable();
            if (visitedMap.Contains(sourceTable))
                return ((DataTable)visitedMap[sourceTable]);


            DataTable destinationTable = ds.Tables[sourceTable.TableName, sourceTable.Namespace];

            if ((destinationTable != null && destinationTable.Columns.Count > 0)) {
                destinationTable = IncrementalCloneTo(sourceTable,destinationTable);
                   // get extra columns from source into destination , increamental read
            }
            else {
                if (destinationTable == null) {
                    destinationTable = new DataTable();
                    // fxcop: new DataTable values for CaseSensitive, Locale, Namespace will come from CloneTo
                    ds.Tables.Add(destinationTable);
                }
                destinationTable = sourceTable.CloneTo(destinationTable, ds, true);
            }
            visitedMap[sourceTable] = destinationTable;


            // start cloning relation
            foreach( DataRelation r in sourceTable.ChildRelations ) {
                DataTable childTable = CloneHierarchy((DataTable)r.ChildTable, ds, visitedMap);
             }

            return destinationTable;
         }
All Usage Examples Of System.Data.DataTable::CloneTo
DataTable