public static byte[] SerializeBinary(object[] obj) { if (obj == null || obj.Length == 0) { return new byte[] {} } ; for (int i = 0; i < obj.Length; i++) { if (obj[i] != null) { if (obj[i] is DataSet) { obj[i] = new DataSetSurrogate((DataSet)obj[i]); } else if (obj[i] is DataTable) { obj[i] = new DataTableSurrogate((DataTable)obj[i]); } } } BinaryFormatter se = new BinaryFormatter(); MemoryStream memStream = new MemoryStream(); se.Serialize(memStream, obj); byte[] bobj = memStream.ToArray(); memStream.Close(); return(bobj); }
/* * Reads the data into the dataset from the DataSetSurrogate object. */ public void ReadDataIntoDataSet(DataSet ds) { if (ds == null) { throw new ArgumentNullException("The dataset parameter cannot be null"); } //Suppress read-only columns and constraint rules when loading the data ArrayList readOnlyList = SuppressReadOnly(ds); ArrayList constraintRulesList = SuppressConstraintRules(ds); //Rows Debug.Assert(IsSchemaIdentical(ds)); Debug.Assert(_dataTableSurrogates != null); Debug.Assert(ds.Tables.Count == _dataTableSurrogates.Length); bool enforceConstraints = ds.EnforceConstraints; ds.EnforceConstraints = false; for (int i = 0; i < ds.Tables.Count; i++) { DataTable dt = ds.Tables[i]; DataTableSurrogate dataTableSurrogate = _dataTableSurrogates[i]; dataTableSurrogate.ReadDataIntoDataTable(ds.Tables[i], false); } ds.EnforceConstraints = enforceConstraints; //Reset read-only columns and constraint rules back after loading the data ResetReadOnly(ds, readOnlyList); ResetConstraintRules(ds, constraintRulesList); }
/* * Reads the schema into the dataset from the DataSetSurrogate object. */ public void ReadSchemaIntoDataSet(DataSet ds) { if (ds == null) { throw new ArgumentNullException("The dataset parameter cannot be null"); } //DataSet properties ds.DataSetName = _datasetName; ds.Namespace = _namespace; ds.Prefix = _prefix; ds.CaseSensitive = _caseSensitive; ds.Locale = _locale; ds.EnforceConstraints = _enforceConstraints; //Tables, Columns Debug.Assert(_dataTableSurrogates != null); foreach (DataTableSurrogate dataTableSurrogate in _dataTableSurrogates) { DataTable dt = new DataTable(); dataTableSurrogate.ReadSchemaIntoDataTable(dt); ds.Tables.Add(dt); } //ForeignKeyConstraints SetForeignKeyConstraints(ds, _fkConstraints); //Relations SetRelations(ds, _relations); //Set ExpressionColumns Debug.Assert(_dataTableSurrogates != null); Debug.Assert(ds.Tables.Count == _dataTableSurrogates.Length); for (int i = 0; i < ds.Tables.Count; i++) { DataTable dt = ds.Tables[i]; DataTableSurrogate dataTableSurrogate = _dataTableSurrogates[i]; dataTableSurrogate.SetColumnExpressions(dt); } //ExtendedProperties Debug.Assert(_extendedProperties != null); if (_extendedProperties.Keys.Count > 0) { foreach (object propertyKey in _extendedProperties.Keys) { ds.ExtendedProperties.Add(propertyKey, _extendedProperties[propertyKey]); } } }
/* * Constructs a DataSetSurrogate object from a DataSet. */ public DataSetSurrogate(DataSet ds) { if (ds == null) { throw new ArgumentNullException("The parameter dataset is null"); } //DataSet properties _datasetName = ds.DataSetName; _namespace = ds.Namespace; _prefix = ds.Prefix; _caseSensitive = ds.CaseSensitive; _locale = ds.Locale; _enforceConstraints = ds.EnforceConstraints; //Tables, Columns, Rows _dataTableSurrogates = new DataTableSurrogate[ds.Tables.Count]; for (int i = 0; i < ds.Tables.Count; i++) { _dataTableSurrogates[i] = new DataTableSurrogate(ds.Tables[i]); } //ForeignKeyConstraints _fkConstraints = GetForeignKeyConstraints(ds); //Relations _relations = GetRelations(ds); //ExtendedProperties _extendedProperties = new Hashtable(); if (ds.ExtendedProperties.Keys.Count > 0) { foreach (object propertyKey in ds.ExtendedProperties.Keys) { _extendedProperties.Add(propertyKey, ds.ExtendedProperties[propertyKey]); } } }