System.Data.DataSet.Clone C# (CSharp) Method

Clone() private method

private Clone ( ) : DataSet
return DataSet
        public virtual DataSet Clone()
        {
            long logScopeId = DataCommonEventSource.Log.EnterScope("<ds.DataSet.Clone|API> {0}", ObjectID);
            try
            {
                DataSet ds = (DataSet)Activator.CreateInstance(GetType(), true);

                if (ds.Tables.Count > 0)  // To clean up all the schema in strong typed dataset.
                {
                    ds.Reset();
                }

                //copy some original dataset properties
                ds.DataSetName = DataSetName;
                ds.CaseSensitive = CaseSensitive;
                ds._culture = _culture;
                ds._cultureUserSet = _cultureUserSet;
                ds.EnforceConstraints = EnforceConstraints;
                ds.Namespace = Namespace;
                ds.Prefix = Prefix;
                ds.RemotingFormat = RemotingFormat;
                ds._fIsSchemaLoading = true; //delay expression evaluation

                // ...Tables...
                DataTableCollection tbls = Tables;
                for (int i = 0; i < tbls.Count; i++)
                {
                    DataTable dt = tbls[i].Clone(ds);
                    dt._tableNamespace = tbls[i].Namespace; // hardcode the namespace for a second to not mess up
                    // DataRelation cloning.
                    ds.Tables.Add(dt);
                }

                // ...Constraints...
                for (int i = 0; i < tbls.Count; i++)
                {
                    ConstraintCollection constraints = tbls[i].Constraints;
                    for (int j = 0; j < constraints.Count; j++)
                    {
                        if (constraints[j] is UniqueConstraint)
                        {
                            continue;
                        }

                        ForeignKeyConstraint foreign = constraints[j] as ForeignKeyConstraint;
                        if (foreign.Table == foreign.RelatedTable)
                        {
                            continue;// we have already added this foreign key in while cloning the datatable
                        }

                        ds.Tables[i].Constraints.Add(constraints[j].Clone(ds));
                    }
                }

                // ...Relations...
                DataRelationCollection rels = Relations;
                for (int i = 0; i < rels.Count; i++)
                {
                    DataRelation rel = rels[i].Clone(ds);
                    rel.CheckMultipleNested = false; // disable the check for multiple nested parent
                    ds.Relations.Add(rel);
                    rel.CheckMultipleNested = true; // enable the check for multiple nested parent
                }

                // ...Extended Properties...
                if (_extendedProperties != null)
                {
                    foreach (object key in _extendedProperties.Keys)
                    {
                        ds.ExtendedProperties[key] = _extendedProperties[key];
                    }
                }

                foreach (DataTable table in Tables)
                {
                    foreach (DataColumn col in table.Columns)
                    {
                        if (col.Expression.Length != 0)
                        {
                            ds.Tables[table.TableName, table.Namespace].Columns[col.ColumnName].Expression = col.Expression;
                        }
                    }
                }

                for (int i = 0; i < tbls.Count; i++)
                {
                    ds.Tables[i]._tableNamespace = tbls[i]._tableNamespace; // undo the hardcoding of the namespace
                }

                ds._fIsSchemaLoading = false; //reactivate column computations

                return ds;
            }
            finally
            {
                DataCommonEventSource.Log.ExitScope(logScopeId);
            }
        }

Usage Example

        private void Load_catesearch()
        {
            var list = per.Load_danhmuc_search(1);
            if (list.Count > 0)
            {
                DataRelation relCat;
                DataTable tbl = DataUtil.LINQToDataTable(list);
                DataSet ds = new DataSet();
                ds.Tables.Add(tbl);

                tbl.PrimaryKey = new DataColumn[] { tbl.Columns["CAT_ID"] };
                relCat = new DataRelation("Category_parent", ds.Tables[0].Columns["CAT_ID"], ds.Tables[0].Columns["CAT_PARENT_ID"], false);

                ds.Relations.Add(relCat);
                DataSet dsCat = ds.Clone();
                DataTable CatTable = ds.Tables[0];

                DataUtil.TransformTableWithSpace(ref CatTable, dsCat.Tables[0], relCat, null);

                Drcate_search.DataSource = dsCat.Tables[0];
                Drcate_search.DataTextField = "CAT_NAME";
                Drcate_search.DataValueField = "CAT_ID";
                Drcate_search.DataBind();
            }
            ListItem l = new ListItem("Tất cả", "0");
            l.Selected = true;
            Drcate_search.Items.Insert(0, l);
        }
All Usage Examples Of System.Data.DataSet::Clone