System.Data.Merger.MergeDataSet C# (CSharp) Метод

MergeDataSet() приватный Метод

private MergeDataSet ( DataSet source ) : void
source DataSet
Результат void
        internal void MergeDataSet(DataSet source)
        {
            if (source == _dataSet) return;  //somebody is doing an 'automerge'
            bool fEnforce = _dataSet.EnforceConstraints;
            _dataSet.EnforceConstraints = false;
            _IgnoreNSforTableLookup = (_dataSet._namespaceURI != source._namespaceURI); // if two DataSets have different 
            // Namespaces, ignore NS for table lookups as we wont be able to find the right tables which inherits its NS

            List<DataColumn> existingColumns = null;// need to cache existing columns

            if (MissingSchemaAction.Add == _missingSchemaAction)
            {
                existingColumns = new List<DataColumn>(); // need to cache existing columns
                foreach (DataTable dt in _dataSet.Tables)
                {
                    foreach (DataColumn dc in dt.Columns)
                    {
                        existingColumns.Add(dc);
                    }
                }
            }


            for (int i = 0; i < source.Tables.Count; i++)
            {
                MergeTableData(source.Tables[i]); // since column expression might have dependency on relation, we do not set
                //column expression at this point. We need to set it after adding relations
            }

            if (MissingSchemaAction.Ignore != _missingSchemaAction)
            {
                // Add all independent constraints
                MergeConstraints(source);

                // Add all relationships
                for (int i = 0; i < source.Relations.Count; i++)
                {
                    MergeRelation(source.Relations[i]);
                }
            }

            if (MissingSchemaAction.Add == _missingSchemaAction)
            {
                // for which other options we should add expressions also?
                foreach (DataTable sourceTable in source.Tables)
                {
                    DataTable targetTable;
                    if (_IgnoreNSforTableLookup)
                    {
                        targetTable = _dataSet.Tables[sourceTable.TableName];
                    }
                    else
                    {
                        targetTable = _dataSet.Tables[sourceTable.TableName, sourceTable.Namespace];// we know that target table wont be null since MissingSchemaAction is Add , we have already added it!
                    }

                    foreach (DataColumn dc in sourceTable.Columns)
                    {
                        // Should we overwrite the previous expression column? No, refer to spec, if it is new column we need to add the schema
                        if (dc.Computed)
                        {
                            DataColumn targetColumn = targetTable.Columns[dc.ColumnName];
                            if (!existingColumns.Contains(targetColumn))
                            {
                                targetColumn.Expression = dc.Expression;
                            }
                        }
                    }
                }
            }

            MergeExtendedProperties(source.ExtendedProperties, _dataSet.ExtendedProperties);
            foreach (DataTable dt in _dataSet.Tables)
            {
                dt.EvaluateExpressions();
            }
            _dataSet.EnforceConstraints = fEnforce;
        }

Usage Example

Пример #1
0
        /// <devdoc>
        ///    <para>
        ///       Merges this <see cref='System.Data.DataSet'/> into a specified <see cref='System.Data.DataSet'/> preserving changes according to
        ///       the specified argument, and handling an incompatible schema according to the
        ///       specified argument.
        ///    </para>
        /// </devdoc>
        public void Merge(DataSet dataSet, bool preserveChanges, MissingSchemaAction missingSchemaAction) {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataSet.Merge|API> %d#, dataSet=%d, preserveChanges=%d{bool}, missingSchemaAction=%d{ds.MissingSchemaAction}\n", ObjectID, (dataSet != null) ? dataSet.ObjectID : 0, preserveChanges, (int)missingSchemaAction);
            try {

                // Argument checks
                if (dataSet == null)
                    throw ExceptionBuilder.ArgumentNull("dataSet");

                switch (missingSchemaAction) { // @perfnote: Enum.IsDefined
                    case MissingSchemaAction.Add:
                    case MissingSchemaAction.Ignore:
                    case MissingSchemaAction.Error:
                    case MissingSchemaAction.AddWithKey:
                        Merger merger = new Merger(this, preserveChanges, missingSchemaAction);
                        merger.MergeDataSet(dataSet);
                        break;
                    default:
                        throw Common.ADP.InvalidMissingSchemaAction(missingSchemaAction);
                }
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }
All Usage Examples Of System.Data.Merger::MergeDataSet